They're both similar in that they deploy the contract. The difference is that .deploy() behaves like a singleton in that you can access the deployed instance with .deployed(). You typically use .new() in tests to avoid the possibility of any side-effects.
Here's an example:
Example.sol
pragma solidity ^0.4.23;
contract Example {
uint256 public data;
function set(uint256 _data) public {
data = _data;
}
}
Example.spec.js
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')
})
})
})
deployedinbeforeEachand I had no inconsistency issues whatsoever. Could you elaborate on what are the fundamental differences, other than the singleton aspect? Or is that it? – Paul Razvan Berg Nov 20 '18 at 12:19deployedinbeforeEachresets the contract? I just ran a quick test and that was not the case. See my updated answer – Miguel Mota Nov 20 '18 at 16:53beforeEachso I think that's why there are no side-effects. – Paul Razvan Berg Nov 20 '18 at 16:57