defi-wonderland / smock

The Solidity mocking library
MIT License
321 stars 39 forks source link

Mock test a contract using SafeERC20 library with smock #194

Closed jptusername closed 5 months ago

jptusername commented 5 months ago

When I want to test my contract using smock framework:

contract TestSafeERC20 {
    using SafeERC20 for IERC20;
    address public token;

    constructor(address tkn) {
        token = tkn;
    }

    function safeTransfer(address from, uint256 value) external {
        IERC20(token).safeTransferFrom(from, address(this), value);
    }
}

I can test it as:

let fakeSafe;
let mockSafeFactory;
let mockSafe;

 beforeEach(async() => {
        fakeSafe = await smock.fake("TestSafeERC20");

        mockSafeFactory = await smock.mock<TestSafeERC20factory>("TestSafeERC20");
        mockSafe = await mockSafeFactory.deploy(tokenAddress);
})

 describe.only("safe tests", async () => {
        it ("should run this test", async () => {
            // this txn is successful
            await fakeSafe.connect(someSigner).safeTransfer(xaddress, toBN(100))

        })

        it ("should run this too right", async() => {
            // this reverts with `SafeERC20: ERC20 operation did not succeed`
            await mockSafe.connect(someSigner).safeTransfer(xaddress, toBN(100))
        })
    })

Shouldn't mock just behave like fakes in this case?

wei3erHase commented 5 months ago

SafeERC20 is an internal library of YOUR contract, in the end, when you mock the ERC20 it'll be called with just transfer, just make sure to return a true.

jptusername commented 5 months ago

SafeERC20 is an internal library of YOUR contract, in the end, when you mock the ERC20 it'll be called with just transfer, just make sure to return a true.

Ah okay, makes sense, and I get it. Thank you very much!