3

I've got problem with mocking method call. Below is my solidity code and test. I would like to pass mock into withdraw method and check if transfer function was called. But instead I am getting error "Mock on the method is not initialized". Any clue? :)

  function withdraw(IERC20 token, uint256 amount) external onlyOwner {
        token.transfer(msg.sender, amount);
    }

describe('LoanManager', () => {
  const [owner, notOwner] = new MockProvider().getWallets();
  let loanManager;
  
beforeEach(async () => {
    loanManager = await deployContract(owner, LoanManager);
  });
    
it('Owner should be able to withdraw', async () => {
        let token = await deployMockContract(owner, IERC20.abi);
        const amount = 5;
        token.mock.transfer.withArgs(owner.address, amount).returns(true);
        loanManager.withdraw(token.address, amount);
        await expect('transfer').to.be.calledOnContract(token);
    });
});

1 Answer 1

0

I think the issue is most likely because you didn't await when mocking the function.

token.mock.transfer.withArgs(...)

should be

await token.mock.transfer.withArgs(...)

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.