.new()
instructs it to deploy an instance of the contract.
.deployed()
returns the deployed contract on the active network, with a twist. You can use that in a UI or nodejs app that wants the deployed contract, but truffle test
uses a sandbox that is meant to prevent tests from scribbling on production contracts. So, test
goes ahead and deploys a new one, just like new()
, and it pretends that was the deployed contract.
Personally, my habit is to use new()
so I know it is a new instance without reliance on the sleight-of-hand always working as expected. That method strikes me as more explicit and less vulnerable to future breaking changes or bugs in Truffle itself.
There is a good reason for the behavior. Developers might form the habit of using deployed()
everywhere. In tests, it could lead to trouble, so the sandbox idea keeps them out of trouble. I treat this interception like an emergency circuit-breaker that shouldn't be the primary means or sole means of preventing a disaster, so new()
, because that's what I actually want.
In practice, both work and the result is mostly the same. Maybe a kind soul will chime in with subtle differences if any exist.
Hope it helps.