3

I am trying to write unit test cases in truffle framework.I am able to write and execute them. However, whenever I have 2 or more test cases the control jumps from 1 test case to another.

For ex: In my given test script the order of execution (added print statements) are out of sync. I am new to javascript and promise. Please let me know what I am missing

Expected output :
  Contract: B
    ✓ Case1
    ✓ Case2
  2 passing (77ms)
In test case 1
In test case 1--->Result
In test case 2
In test case 2--->Result1
In test case 2--->Result2
In test case 2--->Result3


Actual Output :
Using network 'development'.
  Contract: B
    ✓ Case1
    ✓ Case2
  2 passing (77ms)
In test case 1
In test case 2
In test case 1--->Result
In test case 2--->Result1
In test case 2--->Result2
In test case 2--->Result3

Code attached :

// Load A and B contract
var A = artifacts.require("./A.sol");
var B = artifacts.require("./B.sol");

contract('B', function(accounts) {

   it("Case1", function() {

    A.deployed().then(function(instance) {

    instance.create("Testing1","test1",10,1788825289);

    return instance.getContentAddr.call();

    }).then (function(results){

    var contents = results;
    sg = contents[0];
    return  null;

    }).then(function() {

        console.log("In test case 1");
       return B.at(sg).supply(sg,{from:web3.eth.accounts[1],value:11000000000000000000});

    }).then (function(result) {
        console.log("In test case 1--->Result");
    });
  });

  it("Case2", function() {

    A.deployed().then(function(instance) {

    instance.create("Testing1","test2",10,1788825289);

    return instance.getContentAddr.call();

    }).then (function(results){

        var contents = results;
        sg = contents[0];

        console.log("In test case 2");
        return sg;
    }).then(function(instance) {

       return B.at(instance).supply(instance,{from:web3.eth.accounts[0],value:5000000000000000000});
     }).then(function(result) {

       console.log("In test case 2--->Result1");
        return sg;

    }).then(function(instance) {
        console.log("In test case 2--->Result2");
        return B.at(instance).resupply(web3.eth.accounts[0],5000000000000000000,{from:web3.eth.accounts[0]});
    }).then(function(result) {
        console.log("In test case 2--->Result3");
    });
  });
});
3

This is the way its supposed to work. When interacting with contracts we use callback functions to wait for the response to our request and then give us the ability to do something with that response.

In your example TestCase1 is run, which makes a request to create the Testing1 contract. Creating Testing1 will take an indeterminate amount of time and so rather than just wait for that transaction to be mined and return a response, the test framework moves onto the next task it can complete which is to run TestCase2.

At some point TestCase1 receives a response and can move on to the next thing it can do which is to call the getContractAddr function which in turn uses another callback. This process will continue until all of the callbacks return from all of the test cases. At some point the test will have nothing else to do, but that doesn't mean that all of the callback functions have been fired which is why you see the output like that.

Your test cases are guaranteed to be run in order, but may not complete their callbacks in the expected order.

Truffle uses the Mocha test framework and Chai assertion library

  • Thank you TyndieRock for your explanation. It makes sense. However, Is there any other way I could execute each test unit separately ( Something like adding to a new file or something) . The problem I am facing is I am trying to test supply functionality with coinbase accounts and account values keeps changing since it is jumping between test cases. – sun Apr 6 '17 at 17:59
  • 1
    Truffles contract() function will allow you to run each test separately. From the docs - "Before each contract() function is run, your contracts are redeployed to the running Ethereum client so the tests within it run with a clean contract state." Try moving Case2 out of the first contract function into another or you could create a separate file. – TyndieRock Apr 6 '17 at 18:47
  • The url of docs that @TyndieRock mentioned is not available now, update link. – Bowen Xu Apr 18 '18 at 3:58

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.