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')
    console.log('about to deplyoy 2');
      console.log('about to call add citizen');
      instance.addCitizen.call(accounts[1]).then( function(result){
        console.log('here i am');
        deployer.link(IssuingAuthority, CitizenAccount);


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);




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);
      issuingAuthority = _issuingAuthority;


3 Answers 3


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:


Web3 Docs relating to this


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.


I know this is an old question, but this answer could be useful for other people searching for the same thing.

The issue can also be resolved from the migrations file, but here's a solution for when you want to deploy from truffle tests:

    var content = JSON.parse(fs.readFileSync("build/contracts/CitizenAccount.json"));
    var abi = content['abi'];
    var bytecode = content['bytecode'];

    var CitizenAccountInit = new web3.eth.Contract(abi);
    var CitizenAccountTx = CitizenAccountInit.deploy({data: bytecode, arguments: [ia.address]});

    var accounts = await web3.eth.getAccounts();
    var instance = await ContractTx.send({from: accounts[0], gas: 5000000});

    var caAddress = instance.options.address;
    caInstance = new web3.eth.Contract(abi,caAddress);

You need web3.js and fs modules installed for this. The solution is explained in more detail here https://blockheroes.dev/deploy-from-truffle-tests/.

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.