2

This is a follow-up question to How can I use deployed with a constructor argument

For testing purposes with mocha, I would like to configure my default deployed contract so that it is instantiated with the correct parameter. Is there a way to configure truffle / pudding so that the contract is always deployed with a certain parameter value?

1 Answer 1

4

I'm afraid you can't do it easily (but you can set up a more complex deploy management).

However, you can just create a new instance in a test just by calling

Contract.new(params).then(...)  

(Where 'Contract' is the name of a specific contract class name, like in Ballot.new() or MetaCoin.new() ). You could set up a contract instance in the before() function of your test and then just use that instance to test (you should set some contract() scope vars in the before() call and then test them within it() tests).

I.E:

var expect      = require('chai').expect;

var log = console.log;

function clearBytes32(str){
    return str.replace(/\u0000/g, '');
}

function toInteger( bigNumber){
    return parseInt(bigNumber.toString() );
}


contract("Ballot" , function(accounts){
var ballot = null;

describe("new()" , function(){

    var chairperson = null;
    var proposalsCount = null;
    var firstProposal = null;

    before(function(done){

        Ballot.new(["hard fork" , "soft fork" , "do nothing"])
        .catch(log)
        .then(function(contract){
            ballot = contract;
        })
        .catch(log)
        .then( function(){
            return ballot.chairperson();
        })
        .catch(log)
        .then(function(response){
            chairperson = response;
            return ballot.getProposal(0);
        })
        .catch(log})
        .then(function(response){
            firstProposal = response;
            return ballot.proposalsCount();
        })
        .catch(log)
        .then(function(response){
            proposalsCount = response;
        })
        .then(done);

    });


    it("ballot should be set" , function(){
        expect(ballot).to.not.be.null;
    } );

    it("ballot chairperson should be coinbase" , function(){
        expect(chairperson).to.be.equal(accounts[0]);
    });

    it("ballot first proposal should be set" , function(){
        var ascii = web3.toAscii(firstProposal[0]);
        expect(clearBytes32(ascii)).to.be.equal("hard fork");
    });

    it("ballot proposals count should be 3" , function(){
        expect( toInteger(proposalsCount) ).to.be.equal(3);
    });


    describe("vote()" , function(){
        //ballot instance is available here.

    });

});
1
  • this neeeds to get fixed Apr 24, 2017 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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