I'm testing my contract in Truffle. I enabled the contract to receive ERC721 tokens:
function onERC721Received(address, address _from, uint256 _tokenId, bytes calldata) external override returns(bytes4) {
nftContract = ERC721(msg.sender);
tokenId = _tokenId;
tokenAdded = true;
return 0x150b7a02;
}
Is there a way to simulate a token being sent to this contract using Mocha and Chai?
Outside of EVM (for example in a JS test), there's no way to check a return value of a transaction. Only its status (succeeded/reverted), emitted events (in your case non) and few other metadata. And you can also check return value of a call, as in the assert.equal
statements.
contract('MyContract', () => {
it('receives a token', async () => {
const tx = await myContract.onERC721Received(
'0x123', // address
'0x456', // address _from
1, // uint256 _tokenId
[0x01, 0x02] // bytes calldata
);
assert.equal(tx.receipt.status, true); // tx succeeded
assert.equal(await contract.nftContract, '0x123');
assert.equal((await contract.tokenId).toNumber(), 1);
assert.equal(await contract.tokenAdded, true);
});
});
Docs:
I do test this using an ERC721 mock contract in my tests too. So I deploy both contracts and create new instances for them, then from the ERC721 call the mint function to the contract under test address. Then check the balance of ERC721 with:
ERC721.balanceOf(underTest.address, 1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.