Skip to content

Instantly share code, notes, and snippets.

@panukettu
Created September 22, 2023 18:42
Show Gist options
  • Save panukettu/fb82998a58c54b53399f66918ff361d8 to your computer and use it in GitHub Desktop.
Save panukettu/fb82998a58c54b53399f66918ff361d8 to your computer and use it in GitHub Desktop.
Smock API Example
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {MyContract} from "./MyContract.sol";
library MockMyContract {
function mockCall(
function(address) external view returns (uint256),
uint256 mockReturnValue
) internal {
// do the thing to return mockReturnValue
}
function mockCall(
function(address, address) external view returns (uint256),
uint256 mockReturnValue
) internal {
// do the thing to return mockReturnValue when called
}
function mockCall(
function(MyContract.Foo memory)
external
pure
returns (MyContract.Foo memory),
MyContract.Foo memory mockReturnValue
) internal {
// do the thing to return mockReturnValue when calling bar
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MyContract {
struct Foo {
uint256 a;
uint256 b;
}
function balanceOf(address a) external view returns (uint256) {
return 0;
}
function allowance(address a, address b) external view returns (uint256) {
return 0;
}
function bar(Foo memory foo) external pure returns (Foo memory) {
return foo;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {MockMyContract} from "./Mocks.sol";
import {MyContract} from "./MyContract.sol";
import {Test} from "forge-std/Test.sol";
contract MyContractTest is Test {
using MockMyContract for *;
MyContract internal myContract;
function setup() public {
myContract = new MyContract();
myContract.balanceOf.mockCall(1 ether);
}
function testBalanceOf() public {
assertEq(myContract.balanceOf(address(0)), 1 ether);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment