0

I'm testing my contracts using Truffle, but my functions are not returning the correct uint, despite doing so when testing in Remix. This is my Test.js code:

var EventContract = artifacts.require("./EventContract.sol");
contract ('EventContract', function (accounts){
it("account1 purchases 4 tickets from ticketContract, which had 10 tickets to start with at a cost of 2", function(){
    var event;
    var account1 = accounts[0];

    return EventContract.deployed()
    .then(function(instance){
      event = instance;
        return event.buyTicketsFromContract.call(account1, 2, {from: account1, value: 4});
    })
    .then (function (success){
      console.log("purchased from contract with success: " + success);
      assert.equal(true, success, "bought 2 tickets for 4 ether");
      return event.getContractTicketBalance.call();
    })
    .then (function(contractBalance){
      console.log(contractBalance.toNumber());
// Fails here
      assert.equal(8, contractBalance.toNumber(), "contract ticket balance is not 8");
      return event.getTicketBalance.call(account1);
    })
    .then (function(accountBalance){
      assert.equal(2, accountBalance.toNumber(), "account balance is not 2");
    })
    ;
  })
  ;

It fails because the contract ticket balance is 10 (initial amount), not 8. When using the same functions on Remix, the values returned are correct.

1 Answer 1

2

Drop .call() from this line: return event.buyTicketsFromContract.call(account1, 2, {from: account1, value: 4});

When you say call(), you are explicitly saying this is a read-only request so don't even try to update the blockchain. Consequently, when you check what happened, you find that nothing happened.

Hope it helps.

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.