1

I've defined a truffle migration step as follows:

Deploy my 'main' smart contract, nothing out of the ordinary:

 var UniversityVoting = artifacts.require('./UniversityVoting.sol');

   module.exports = async (deployer) => {

     await deployer.deploy(UniversityVoting);

};

I then deploy an ERC20 token contract, called VotingToken. As you can see, in module.exports, I get an instance of UniversityVoting which I deployed in step 1, so I can then pass the address of the newly deployed VotingToken to it:

var VotingToken = artifacts.require('./VotingToken.sol');
var UniversityVoting = artifacts.require('./UniversityVoting.sol');

module.exports = async (deployer) => {

    UniversityVotingInstance = await UniversityVoting.deployed();

    await deployer.deploy(VotingToken, UniversityVotingInstance.address);

    await UniversityVotingInstance.setVotingTokenAddress(VotingToken.address);

};

This works fine using truffle migrate and I can indeed see the correct VotingToken address in Ganache: enter image description here

But in a test written in JavaScript that I execute using truffle test, where I create a new instance of UniversityVotingand call a getter method that returns deployedVotingToken it returns a 0 address. Why would that happen in a truffle test? I thought it was supposed to run your migration files?

describe("Deploy and use the child institution contract", function() {
  before(async function() {

    universityVoting = await UniversityVoting.new(
      {
      from: developerAccount
    });

...

// In an 'it' block

const originalToken = await universityVoting.getVotingTokenAddress();

originalToken.should.equal("[exampleAddress]");  // Here it says that AssertionError: expected '0x0000000000000000000000000000000000000000' to equal '0x34344343434'
1

This was an elementary mistake from my part. In a truffle test environment, if I have to create a new instance of any arbitrary contract, in this case UniversityVoting, why wouldn't I have to with VotingToken? Very silly. So I called a setter and gave the deployedVotingToken instance variable to UniversityVoting and it is no longer unitialised/0 address.

votingToken = await VotingToken.new(
  universityVoting.address,
  {
  from: developerAccount
});
const setToken = await universityVoting.setVotingTokenAddress(
  votingToken.address,
  { from: developerAccount }
);
| improve this answer | |
  • 1
    Since you are deploying a separate instance the migrations scripts are not called on it. If you have used UniversityVoting.deployed() instead it will be initialized. – Ismael Jul 16 '19 at 20:31
  • I wasn't going to comment because rules but this is a gamechanger, thanks. If you put that as an answer I'll accept it! – E. Rowlands Jul 16 '19 at 20: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.