0

I want to test some contracts^^ And now Im wondering because like I understand .deployed() should take the contracts from the migration in truffle and dont deploy new ones?

My test.js:

const one = artifacts.require("One.sol");
 const two= artifacts.require("Two.sol");

 contract('One and Two', async (accounts) => {

    let instance;

    let deployAccount = accounts[1];
    
    before(async () => {
        ione= await one.deployed();
        itwo= await two.deployed();
    })
    it("Two: should return the owner", async ()=> {
        const value = await itwo.getOwner.call();
    
        assert.equal(value, deployAccount);
    });

In my ganache browser I can see that always I run truffle test there will be 3 contracts deployed... (the default contract Migration.sol is the 3rd) But I only see the contracts that get deployed with "truffle migration"

So my question is:

Is it possible to dont deploy by running "truffle test" without deploying contracts that allready in ganache? Or is it possible to update ganache to see the last contract in the writer "contracts" that have been deployed?

0

You are correct. When using .deployed() you basically retrieve the contract that has been deployed by your Migrations (you can see the deployed contract address in your generated ABI under /build/YourContract.json).

However when you test with Ganache then Truffle creates what they call a "Cleam Room Environment". This basically means that Truffle will re-deploy all of your migrations at the beginning of every test file to ensure you have a fresh set of contracts to test against.

If you want to use your previous contracts and not re-deploy them automatically, you could have your Migration use the overwrite:false option:

// Don't deploy this contract if it has already been deployed
deployer.deploy(A, {overwrite: false});
3
  • Thanks for the fast answere I added: const one = artifacts.require("one"); module.exports = function(deployer, network, accounts) { deployer.deploy(one, { override:false, from: accounts[1] }); } to my migration file but it still the same behavior. The contracts get everytime deployed when I run "truffle test"
    – M Token
    Dec 19 '20 at 14:53
  • @MToken I seem to be having issues reproducing the override:false too... What you could do instead of using deployed() is to deploy your contracts first and then set the address like this ione = await one.at("0x....")
    – Valdi_K
    Dec 21 '20 at 12:33
  • Thank you will check that
    – M Token
    Dec 21 '20 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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