0

I have a few contracts that I wanted to test using Mocha (that comes with Truffle). These contracts all require parameters in their constructor. I have modified the "2_deploy_contracts.js" file and it works as per requirement.

How do I code my mocha test suite so that I can invoke my customised 2_deploy_contracts.js? It seems the clean-room of Truffle test only works with simple zero-parameter constructors.

1

When making tests with truffle test you can request for deploying contract in the testfile Here is an example of a series of tests i wrote to give an example :

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

// Deploy an instance of the contract 
contract('Organizations',function(accounts){
    it("Owner on deployment should be : " + accounts[0],function(){
        return Organizations.new("GroupTest",accounts[0],{from : accounts[0], gas : '2500000'}).then(function(inst){
            return inst.owner();
        }).then(function(r){
            assert.equal(r,accounts[0],"Owner is not : " + accounts[0]);
        });
    });

// Check data keeps coherence  : TODO 
    it("Users should be able to request membership",function(){
        return Organizations.new("GroupTest",accounts[0],{from : accounts[0], gas : '2500000'}).then(function(inst){
           return inst.requestMembership({from: accounts[1], gas : 150000}).then(function(){
               return inst.membershipRequests(accounts[1]);
           }).then(function(r){
                assert.equal(r,true,"User hasn\'t been registered in membership request registry");    
           });
        });
    });

Aknowledging the following parts of the contract :

contract Organizations{
    string public name;
    address public owner;
    mapping(address => bool) public membershipRequests;

    function Organizations(string _name, address _owner){
        owner = _owner;
        name = _name;
    }

    function requestMembership() returns(bool success){
        membershipRequests[msg.sender] = true;
        return true;
    }

}
  • If I invoke the constructor within the test suite, will Truffle knows it and therefore skip its own deployment? If the deployment is now a test case, is there a easy way for me to define the dependency so that other test cases only run after the deployment is completed? – Thomas Feb 7 '18 at 5:29

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.