2

Well, I have a smart contract called Register. The test script is quite straightforward:

const Register = artifacts.require("Register")

contract('Register', (accounts) =>  {
    let register;

    beforeEach('create new Register contract', async () => {
        register = await Register.new("test", 0, {from: accounts[0]});
    })

    it("test register enntity", async () => {
        register.entity().then(entity => {
            assert.equal(entity, accounts[0], "Entity is wrong!");
        })
    })
})

If I create 2_migrate_contracts.js in folder migrations with content:

var Register = artifacts.require("./Register.sol");

module.exports = function(deployer) {
  deployer.deploy(Register, "tesst", 0);
};

then the truffle test process finish with no error.

As far as I know, the test script creates a new Register contract, which should be irrelevant with the migrated contract. So if I comment out the 2_migrate_contracts.js file and let the test script do all the jobs. However, I got this error:

"before all" hook: prepare suite:
 Uncaught Error: Attempting to run transaction which calls a contract function, but recipient address 0x4f94fdfc235abc73a163bff1ba9341f6226ff6bd is not a contract address.

I tried to delete the ./build folder. No help.

So I am confused. Does truffle test have to begin with a migration process? i.e., I can not skip the migration process and created new contract in test script only?

Thanks in advance.

=================== UPDATE ======================

I just take a step forward. In my project, actually, there is another contract named Authorization, and accordingly, there is another test file named TestAuthorization. Now if I run the test file by file such as:

truffle test ./test/TestRegister

Then the test will be fine. But if I run all the test together using truffle test. Then the 'recipient address is not a contract address' error shows again.

According to the Josesph's reply, it is said When running your tests against Ganache or Truffle Develop, Truffle will use advanced snapshotting features to ensure your test files don't share state with each other. Now the question is: how can I run multiple test with truffle test?

2 Answers 2

3

Does truffle test have to begin with a migration process? i.e., I can not skip the migration process and created new contract in test script only?

Yes, you can.

In your 2_migrate_contracts.js script, change this:

module.exports = function(deployer) {
  deployer.deploy(Register, "tesst", 0);
};

To this:

module.exports = function(deployer, network, accounts) {
    if (network == "production")
        deployer.deploy(Register, "tesst", 0);
};

And in your truffle.js (truffle configuration) file, do something like:

module.exports = {
    networks: {
        development: {
            host:       "127.0.0.1",
            network_id: "*",
            port:       8545,
            gas:        4712388,
            gasPrice:   100000000000
        },
        production: {
            host:       "127.0.0.1",
            network_id: "*",
            port:       8545,
            gas:        4712388,
            gasPrice:   100000000000
        }
    },
    ...
}
1

The contracts created during the truffle test process will be deleted once the process finishes. This is because truffle runs it against testrpc instance it creates while the testing is started. This instance is closed down after the testing finishes.

Check below https://truffleframework.com/docs/truffle/testing/testing-your-contracts#clean-room-environment

Migration process is still needed as instructed in the documentation.

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.