-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test from readme in mockContract (#375)
* Bump waffle version in the README * Add a test from the README * Update example test * Change setup function into a beforeEach Co-authored-by: Marek Kirejczyk <marekkirejczyk@users.noreply.github.com>
- Loading branch information
1 parent
c60acfe
commit 8c5942d
Showing
3 changed files
with
92 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {use, expect} from 'chai'; | ||
import {Contract, ContractFactory, utils, Wallet} from 'ethers'; | ||
import {MockProvider} from '@ethereum-waffle/provider'; | ||
import {waffleChai} from '@ethereum-waffle/chai'; | ||
import {deployMockContract} from '../src'; | ||
|
||
import IERC20 from './helpers/interfaces/IERC20.json'; | ||
import AmIRichAlready from './helpers/interfaces/AmIRichAlready.json'; | ||
|
||
use(waffleChai); | ||
|
||
describe('Am I Rich Already', () => { | ||
let contractFactory: ContractFactory; | ||
let sender: Wallet; | ||
let receiver: Wallet; | ||
let mockERC20: Contract; | ||
let contract: Contract; | ||
|
||
beforeEach(async () => { | ||
[sender, receiver] = new MockProvider().getWallets(); | ||
mockERC20 = await deployMockContract(sender, IERC20.abi); | ||
contractFactory = new ContractFactory(AmIRichAlready.abi, AmIRichAlready.bytecode, sender); | ||
contract = await contractFactory.deploy(mockERC20.address); | ||
}); | ||
|
||
it('returns false if the wallet has less then 1000000 coins', async () => { | ||
await mockERC20.mock.balanceOf.returns(utils.parseEther('999999')); | ||
expect(await contract.check()).to.be.equal(false); | ||
}); | ||
|
||
it('returns true if the wallet has at least 1000000 coins', async () => { | ||
await mockERC20.mock.balanceOf.returns(utils.parseEther('1000001')); | ||
expect(await contract.check()).to.equal(true); | ||
}); | ||
|
||
it('reverts if the ERC20 reverts', async () => { | ||
await mockERC20.mock.balanceOf.reverts(); | ||
await expect(contract.check()).to.be.revertedWith('Mock revert'); | ||
}); | ||
|
||
it('returns 1000001 coins for my address and 0 otherwise', async () => { | ||
await mockERC20.mock.balanceOf.returns('0'); | ||
await mockERC20.mock.balanceOf.withArgs(sender.address).returns(utils.parseEther('1000001')); | ||
|
||
expect(await contract.check()).to.equal(true); | ||
expect(await contract.connect(receiver.address).check()).to.equal(false); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
waffle-mock-contract/test/helpers/contracts/AmIRichAlready.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
pragma solidity ^0.6.3; | ||
|
||
interface IERC20 { | ||
function balanceOf(address account) external view returns (uint256); | ||
} | ||
|
||
contract AmIRichAlready { | ||
IERC20 private tokenContract; | ||
uint public richness = 1000000 * 10 ** 18; | ||
|
||
constructor (IERC20 _tokenContract) public { | ||
tokenContract = _tokenContract; | ||
} | ||
|
||
function check() public view returns (bool) { | ||
uint balance = tokenContract.balanceOf(msg.sender); | ||
return balance > richness; | ||
} | ||
|
||
function setRichness(uint256 _richness) public { | ||
richness = _richness; | ||
} | ||
} |