-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(test): migrate unit tests to the new motsu layout #423
base: main
Are you sure you want to change the base?
Conversation
…ontract-deployment
✅ Deploy Preview for contracts-stylus canceled.
|
contracts/src/utils/context.rs
Outdated
#[cfg(test)] | ||
pub fn msg_sender() -> Address { | ||
motsu::prelude::Context::current() | ||
.get_msg_sender() | ||
.expect("msg_sender should be set") | ||
} | ||
|
||
/// Returns the address of the message sender. | ||
#[cfg(not(test))] | ||
pub fn msg_sender() -> Address { | ||
msg::sender() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is an example how msg_sender
mock can be declared. This design is not perfect since motsu users should implement this mock for their own library manually or through macro.
Calling stylus sdk api msg::sender()
won't work inside a test without mock due to this limitation.
contracts/src/token/erc721/mod.rs
Outdated
#[motsu::test] | ||
fn on_erc721_received( | ||
erc721: Contract<Erc721>, | ||
receiver: Contract<Erc721ReceiverMock>, | ||
) { | ||
let alice = Account::random(); | ||
let token_id = random_token_id(); | ||
erc721 | ||
.sender(alice) | ||
._safe_mint(receiver.address(), token_id, vec![0, 1, 2, 3].into()) | ||
.unwrap(); | ||
|
||
let received_token_id = receiver.sender(alice).received_token_id(); | ||
|
||
assert_eq!(received_token_id, token_id); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall the test case for Erc721Receiver
can look like this
This pr contains migration of unit test to the new layout of motsu unit tests, that will be introduced in OpenZeppelin/stylus-test-helpers#8 and OpenZeppelin/stylus-test-helpers#12
Resolves #470
PR Checklist