Skip to content

Conversation

@tekvyy
Copy link
Contributor

@tekvyy tekvyy commented Nov 21, 2025

High Level Overview of Change

Context of Change

Currently, testing code that depends on host functions is difficult to test because the host bindings return hardcoded values in test mode.
This makes it impossible to test error handling paths or specific return values.

The solution:
A lightweight, macro-based mocking system that allows tests to control host function behavior without modifying production code.
The mock infrastructure:

  • Uses thread-local storage for test isolation
  • Works only in #[cfg(test)] mode (zero runtime overhead)
  • Provides a simple mock_host! macro for setup
  • Maintains existing behavior when no mock is set

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (non-breaking change that only restructures code)
  • Tests (You added tests for code that already exists, or your new feature included in this PR)
  • Documentation Updates
  • Release

Test Plan

  1. Run cargo test

Future Tasks

If this approach is approved:

  • Expand mock infrastructure to other host function groups (ledger, transaction, crypto, etc.)
  • Add more comprehensive test coverage using the mock infrastructure

@tekvyy
Copy link
Contributor Author

tekvyy commented Nov 21, 2025

@mvadari @sappenin Please review the approach. Let me know

Copy link
Collaborator

@mvadari mvadari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm understanding this approach right, you have to clear mocks at the end of each test, right? Is it possible to do that automatically upon the end of the test, perhaps via Drop?

@tekvyy
Copy link
Contributor Author

tekvyy commented Nov 21, 2025

@mvadari Yes currently manual clear is required, Looking into the Drop approach. Thank you
Will push once its ready.

@tekvyy
Copy link
Contributor Author

tekvyy commented Nov 22, 2025

@mvadari @sappenin Pushed auto cleanup via Drop Trait. Thanks

Copy link
Collaborator

@mvadari mvadari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sappenin can you take a look at this? I'm pretty happy with it, but I think this one should get an approval from both of us before @tekvyy applies this to the rest of the host functions


let result = unsafe { super::super::get_nft_transfer_fee(ptr::null(), 0) };
assert_eq!(result, 42);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a test to check actually passing some data into the pointer (the way the host functions actually work)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bump @tekvyy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mvadari Pushed the test, please review

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mvadari let me know if anything else is needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mvadari Let me know if we can merge this, i will create a followup PR with tests for other modules :)

Copy link
Collaborator

@sappenin sappenin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tekvyy - thanks for submitting this PR. It's been on my TODO list for a while to review, but I hadn't had time until yesterday (apologies for that delay).

After thinking things through yesterday, I've become convinced that we should use a proper mocking library for our own unit testing in this library (rather than create our own using macros, which will likely work in the shorter term, but be more difficult to maintain in the longer term).

To that end, I created a series of 3 draft PRs as a sort of "alternative route" for mocking using a well-known mocking library (mockall).

Take a look at #117 and let me know what you think? If you're agreeable, I suggest we close this PR in-favor of #117, #118, and #119.

@tekvyy
Copy link
Contributor Author

tekvyy commented Dec 17, 2025

Hi @tekvyy - thanks for submitting this PR. It's been on my TODO list for a while to review, but I hadn't had time until yesterday (apologies for that delay).

After thinking things through yesterday, I've become convinced that we should use a proper mocking library for our own unit testing in this library (rather than create our own using macros, which will likely work in the shorter term, but be more difficult to maintain in the longer term).

To that end, I created a series of 3 draft PRs as a sort of "alternative route" for mocking using a well-known mocking library (mockall).

Take a look at #117 and let me know what you think? If you're agreeable, I suggest we close this PR in-favor of #117, #118, and #119.

Hey @sappenin
Thanks for the review and the thoughtful explanation.
Using an established library like mockall makes sense for long-term maintainability.
I'll take a look at #117 and the related PRs.
Closing this one.

@tekvyy tekvyy closed this Dec 17, 2025
@tekvyy tekvyy deleted the feature/host_mocking branch December 17, 2025 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants