doppelgänger /ˈdɒp(ə)lˌɡaŋə,ˈdɒp(ə)lˌɡɛŋə/ - an apparition or double of a living person
Library for mocking smart contract dependencies during unit testing.
Install
To start using with npm, type:
npm i -D ethereum-doppelganger
or with Yarn:
yarn add --dev ethereum-doppelganger
Doppelganger is currently developed to work with ethers-js exclusively. Support for other framework will be added in a future version.
Usage
Create a instance of Doppelganger
providing the ABI/interface of the smart contract you want to mock:
; ... const doppelganger = abi;
Deploy a instance of the Doppelganger smart contract:
await doppelganger;
Doppelganger can now be passed into other contracts by using the address
attribute.
Return values for mocked functions can be set using:
await doppelganger<nameOfMethod>
Example
Below example illustrates how Doppelganger can be used to test the very simple AmIRichAlready
contract.
pragma solidity ^0.4.24; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; contract AmIRichAlready { IERC20 private tokenContract; address private wallet; uint private constant RICHNESS = 1000000 * 10 ** 18; constructor (IERC20 _tokenContract) public { tokenContract = _tokenContract; wallet = msg.sender; } function check() public view returns(bool) { uint balance = tokenContract.balanceOf(wallet); return balance > RICHNESS; }}
We are mostly interested in the tokenContract.balanceOf
call. Doppelganger will be used to mock exactly this call with values that are significant for the return of the check()
method.
;;;;; ;; chai; ;