-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🗽 Add address parameter to the mock contract (#815)
* 🗽 Add address parameter to the mock contract Co-authored-by: yivlad <[email protected]>
- Loading branch information
1 parent
b54c6b9
commit da92375
Showing
11 changed files
with
274 additions
and
162 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@ethereum-waffle/hardhat": patch | ||
"@ethereum-waffle/mock-contract": patch | ||
--- | ||
|
||
Add mock contract deployment at a specified address |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,15 @@ | ||
import {waffle} from 'hardhat'; | ||
import {MockProvider} from 'ethereum-waffle'; | ||
import {mockContractDirectTest} from '@ethereum-waffle/mock-contract/test/directTest'; | ||
import {mockContractProxiedTest} from '@ethereum-waffle/mock-contract/test/proxiedTest'; | ||
|
||
describe('INTEGRATION: Mock Contract', () => { | ||
const provider = waffle.provider as MockProvider; | ||
|
||
before(async () => { | ||
await provider.send('hardhat_reset', []); | ||
}); | ||
|
||
mockContractDirectTest(provider); | ||
mockContractProxiedTest(provider); | ||
}); |
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
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 |
---|---|---|
@@ -1,102 +1,4 @@ | ||
import {use, expect} from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import {MockProvider} from '@ethereum-waffle/provider'; | ||
import {waffleChai} from '@ethereum-waffle/chai'; | ||
import {ContractFactory} from 'ethers'; | ||
import {mockContractDirectTest} from './directTest'; | ||
|
||
import {deployMockContract} from '../src'; | ||
import Counter from './helpers/interfaces/Counter.json'; | ||
|
||
use(chaiAsPromised); | ||
use(waffleChai); | ||
|
||
describe('Mock Contract - Integration (called directly)', () => { | ||
const [wallet] = new MockProvider().getWallets(); | ||
|
||
it('throws readable error if mock was not set up for a method', async () => { | ||
const mockCounter = await deployMockContract(wallet, Counter.abi); | ||
|
||
await expect(mockCounter.read()).to.be.revertedWith('Mock on the method is not initialized'); | ||
}); | ||
|
||
it('mocking returned values', async () => { | ||
const mockCounter = await deployMockContract(wallet, Counter.abi); | ||
await mockCounter.mock.read.returns(45291); | ||
|
||
expect(await mockCounter.read()).to.equal(45291); | ||
}); | ||
|
||
it('mocking revert', async () => { | ||
const mockCounter = await deployMockContract(wallet, Counter.abi); | ||
await mockCounter.mock.read.reverts(); | ||
|
||
await expect(mockCounter.read()).to.be.revertedWith('Mock revert'); | ||
}); | ||
|
||
it('mock with call arguments', async () => { | ||
const mockCounter = await deployMockContract(wallet, Counter.abi); | ||
await mockCounter.mock.add.returns(1); | ||
await mockCounter.mock.add.withArgs(1).returns(2); | ||
await mockCounter.mock.add.withArgs(2).reverts(); | ||
|
||
expect(await mockCounter.add(0)).to.equal(1); | ||
expect(await mockCounter.add(1)).to.equal(2); | ||
await expect(mockCounter.add(2)).to.be.revertedWith('Mock revert'); | ||
expect(await mockCounter.add(3)).to.equal(1); | ||
}); | ||
|
||
it('should be able to call to another contract', async () => { | ||
const counterFactory = new ContractFactory(Counter.abi, Counter.bytecode, wallet); | ||
const counter = await counterFactory.deploy(); | ||
const mockCounter = await deployMockContract(wallet, Counter.abi); | ||
|
||
expect(await mockCounter.staticcall(counter, 'read()')).to.equal('0'); | ||
expect(await mockCounter.staticcall(counter, 'read')).to.equal('0'); | ||
}); | ||
|
||
it('should be able to call another contract with a parameter', async () => { | ||
const counterFactory = new ContractFactory(Counter.abi, Counter.bytecode, wallet); | ||
const counter = await counterFactory.deploy(); | ||
const mockCounter = await deployMockContract(wallet, Counter.abi); | ||
|
||
expect(await mockCounter.staticcall(counter, 'add', 1)).to.equal('1'); | ||
}); | ||
|
||
it('should be able to call another contract with many parameters', async () => { | ||
const counterFactory = new ContractFactory(Counter.abi, Counter.bytecode, wallet); | ||
const counter = await counterFactory.deploy(); | ||
const mockCounter = await deployMockContract(wallet, Counter.abi); | ||
|
||
expect(await mockCounter.staticcall(counter, 'addThree', 1, 2, 3)).to.equal('6'); | ||
}); | ||
|
||
it('should be able to execute another contract', async () => { | ||
const counterFactory = new ContractFactory(Counter.abi, Counter.bytecode, wallet); | ||
const counter = await counterFactory.deploy(); | ||
const mockCounter = await deployMockContract(wallet, Counter.abi); | ||
|
||
await mockCounter.call(counter, 'increment()'); | ||
expect(await counter.read()).to.equal('1'); | ||
|
||
await mockCounter.call(counter, 'increment'); | ||
expect(await counter.read()).to.equal('2'); | ||
}); | ||
|
||
it('should be able to execute another contract with a parameter', async () => { | ||
const counterFactory = new ContractFactory(Counter.abi, Counter.bytecode, wallet); | ||
const counter = await counterFactory.deploy(); | ||
const mockCounter = await deployMockContract(wallet, Counter.abi); | ||
|
||
await mockCounter.call(counter, 'increaseBy', 2); | ||
expect(await counter.read()).to.equal('2'); | ||
}); | ||
|
||
it('should be able to execute another contract with many parameters', async () => { | ||
const counterFactory = new ContractFactory(Counter.abi, Counter.bytecode, wallet); | ||
const counter = await counterFactory.deploy(); | ||
const mockCounter = await deployMockContract(wallet, Counter.abi); | ||
|
||
await mockCounter.call(counter, 'increaseByThreeValues', 1, 2, 3); | ||
expect(await counter.read()).to.equal('6'); | ||
}); | ||
}); | ||
mockContractDirectTest(new MockProvider()); |
Oops, something went wrong.