4

I'm having trouble getting my test harness set up in truffle. I have 2 contracts. The first is an Issuing authority and in order for the second to be deployed, the creation address must be registered with the first. I'm having a hard time figuring out how to best do this with truffle testing because I can't deploy the contract without it failing in my migration because the contract creation code throws(as expected).

I tried the below in my migration code, but the 'here i am' comment never gets hit. I'm thinking it is because I don't really have a good space to do this in and it would be for moot anyway since each test creates its own little sand box.

var IssuingAuthority = artifacts.require("./IssuingAuthority.sol");
var CitizenAccount = artifacts.require("./CitizenAccount.sol");

module.exports = function(deployer) {
  console.log('about to deploy')
  deployer.deploy(IssuingAuthority).then(function(){
    console.log('about to deplyoy 2');
    IssuingAuthority.deployed().then(function(instance){
      console.log('about to call add citizen');
      instance.addCitizen.call(accounts[1]).then( function(result){
        console.log('here i am');
        console.log(result);
        deployer.link(IssuingAuthority, CitizenAccount);
        console.log(IssuingAuthority.address);
        deployer.deploy(CitizenAccount,IssuingAuthority.address);
      });

    });
  });
}

I'm wondering if I can get access to the deployer inside of a test so that I can deploy my contract to the network at test time. something like this:

  var IssuingAuthority = artifacts.require("./IssuingAuthority.sol");
  var CitizenAccount = artifacts.require("./CitizenAccount.sol");

  contract('CitizenAccount', function(accounts) {
    it("should be authorized", function() {

      var ia = null;
      return IssuingAuthority.deployed().then(function(instance) {
        ia = instance;
        return instance.addCitizen.call(accounts[1]);
      }).then(function(result) {
        assert.equal(result, true, "citizen wasnt added");
      ///////////////////
      // I need to do a deployment here now that the issuer contract 
      // has the address I want.
      /////////////////////////

        return CitizenAccount.deployed(ia.address);
      }).then(function(caInstance){

      });

    });

  });

Here is the code for my citizen account contract. Feel free to point me to a better pattern if this just isn't going to work:

pragma solidity ^0.4.2;

import "./IssuingAuthority.sol";

contract CitizenAccount {
  address public issuingAuthority;
  address owner;

  function CitizenAccount(address _issuingAuthority) {
    IssuingAuthority i = IssuingAuthority(_issuingAuthority);
    bool isCitizen = i.isCitizen(msg.sender);
    if(isCitizen){
      issuingAuthority = _issuingAuthority;
    }
    else{
      throw;
    }
  }

}
8

If I understand correctly, you want to deploy a contract during testing rather than during the migration (initial deployment) stage.

You can do this using the contract abstraction in your test script, like this

 var contractInstance = MyContract.new([contructorParam1], {data: ...});

Or in your case:

CitizenAccount.new(ia.address)

Web3 Docs relating to this

1

There's a code sample using beforeEach() over here: Truffle Smart Contract Testing does not reset state

General idea, tests deploy new contract instances for the it() tests.

You can adapt the pattern to all kinds of situations.

Hope it helps.

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.