3

I have the following javascript test for a simple ERC 20 token. Which on the call return token.balanceOf.call(accounts[2]); will fetch some old data and fail from a previous test transaction writting similar, but in another contract() scope on the same contract. Using TestRpc

contract('ERC Allowance', function (accounts) {

    it("should be able to lend money and that user spend it", function () {
        var token;

        return Token.deployed().then(function (instance) {
            token= instance;
            return instance.approve(accounts[1], 5555, {from: accounts[0]});
        })
            .then(function (approveTransaction) {
                return chftoken .allowance.call(accounts[0], accounts[1]);
            })
            .then(function (allowance) {
                assert.equal(allowance, 5555, "allowance should be increased");
                return token.transferFrom.call(accounts[0], accounts[2], 55555, {from: accounts[1]});
            })
            .then(function (failedTransaction) {
                return token.balanceOf.call(accounts[2]);
            })
            .then(function (balance) {
                assert.equal(balance.valueOf(), 0, "transfer should not go through");
                return token.transferFrom.call(accounts[0], accounts[9], 10000, {from: accounts[1]});
            })
            .then(function (transactionCompleted) {
                return token.balanceOf.call(accounts[2])
            })
            .then(function (balance) {
                assert.equal(balance, 5555, "the balance should be transferred")
            });
    });
});
11

To reset the state, you have to tell the test suite to deploy a new instance of the contract before it starts.

You can tell it to deploy a new contract before each "it" with beforeEach(). Every test will start with an initialized state (and a new contract .deployed() address).

contract( ...

  var contract;

  beforeEach(function() {
     return Contract.new()
     .then(function(instance) {
        contract = instance;
     });
  });

  it("should ...

Hope I didn't flub the syntax. Truffle 3.x style.

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.