1

I use Truffle to test a contract. When i use Contract.deployed() and i fetch address, for example :

  • In a first test, the contract's address is 0x07D899846146b3503dE9D9Dc36B94C2e428eA434
  • In a second one, the contract's address is 0xF79f3B32b78ebD07B2E661b52e9CF374e311f1d5

Another problem is that it seems that on each time i test the contract, it's state is reinitialized.

It seems that Contract.deployed() create works like Contract.new(), it create a new instance of the contract for each test.

Which can cause this problem ?

2
  • When truffle test completes, the next time you run it, the initial migration takes place again and new contracts are deployed. If you've left Ganache opened from the previous execution of truffle test, then those new instances will be deployed at different addresses. Commented Jun 27, 2019 at 15:38
  • So how can I make sure that the contract is always at the same address? How can I test the contract once, a second time by making sure that the state of the contract during the second test is the same as the state of the contract at the end of the first test? Commented Jun 28, 2019 at 2:21

2 Answers 2

2

Actually many similar questions of mine not answered yet (even at official forum of truffle 80% questions not addressed ), which clearly indicates that either truffle developers have lost control over project means patching one side creating problem on other side, or they leave this project as Orphan, so don't expect any logical clear answer for your question, I asked question on many places for simple one no one addressed to the point, some one tried but talking here and there but not to the point, so I hope this will be he perfect answer for your question, Thank You

0

When writing tests for Truffle you use contract() function. As stated in the documentation, this function re-runs your migrations script and Contract.deployed() returns the instance of the contract deployed during the migration.

You can use beforeEach() and deploy new contract there (may be adding some initialization transactions). This way is more explicit and clear, you always see whether you use redeployed contract or the used one.

You do not need to always have the same contract address for the tests as you can use instance of the contract returned by these functions and if you need the contract address you can access it via (probably) instance.address or smth like that. See the truffle-contract documentation for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.