Created
November 20, 2018 16:56
-
-
Save miguelmota/7db698631aebefef7cf8451db3c26abc to your computer and use it in GitHub Desktop.
Truffle deployed() vs new()
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
pragma solidity ^0.4.23; | |
contract Example { | |
uint256 public data; | |
function set(uint256 _data) public { | |
data = _data; | |
} | |
} |
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
const Example = artifacts.require('Example') | |
contract('Example', (accounts) => { | |
let instance | |
beforeEach('setup', async () => { | |
instance = await Example.new() | |
// second test will fail with `deployed()` | |
//instance = await Example.deployed() | |
}) | |
describe('test singleton', () => { | |
it('should set data', async () => { | |
assert.equal(await instance.data.call(), '0') | |
const {receipt: {status}} = await instance.set(5) | |
assert.equal(status, '0x01') | |
assert.equal(await instance.data.call(), '5') | |
}) | |
it('should not read previous data', async () => { | |
// this will be `5` instead of `0` if using `deployed()` | |
assert.equal(await instance.data.call(), '0') | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment