0

I am having trouble unit testing my contracts: I am using Mocha, Chai and calling truffle test

I am testing LoanRecord.sol

contract LoanRecord {
  public bool on;
  ...

  constructor(...){
    ...
    on = false;
   }

  function external turnOn(){
    on = true;
  }
}

And testing in record.test.js:

var assert = require('chai').assert
var LoanRecord = artifacts.require("../../contracts/record/LoanRecord.sol");

contract ('Record', function (accounts) {
    describe("#turnOn()", function () {
        it("should turn on a loan record instance", async function () {
            loanrecord = await LoanRecord.new(
                1,
                18,
                0x0000000000000000000000000000000000000002,
                0x0000000000000000000000000000000000000001, 
                "LOAN"
                );
            await assert.equal(loanrecord.on, false)
            await loanrecord.turnOn()
            await assert.equal(loanrecord.on, true)
        });
    });

However, I am recieving:

1) Contract: Record
       #turnOn()
         should turn on a loan record instance:
     AssertionError: expected [Function] to equal false
      at Context.<anonymous> (test/record.js:15:26)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

Why does loanrecord.on return [Function] rather than a boolean type?

Fixed (with gitter@dwardu help too):

contract ('Record', function (accounts) {
    describe("#turnOn()", function () {
        it("should turn on a loan record instance", async function () {
            loanrecord = await LoanRecord.new(
                1,
                18,
                0x0000000000000000000000000000000000000002,
                0x0000000000000000000000000000000000000001, 
                "LOAN"
                );
            var on = await loanrecord.on()
            await assert.equal(on, false)
            await loanrecord.turnOn()
            var on = await loanrecord.on()
            await assert.equal(on, true)
        });
    });
}
1

The error message you're getting is giving you a good hint about comparing function to a boolean value. This means one side of the comparison is a function and one is a variable (or value).

For each variable in your Solidity contracts, a public getter function is automatically generated. This means a function with signature function on() is automatically generated to access the value of the variable on. Therefore when accessing the variables you also have to access them through the getter functions.

Change your await assert.equal(loanrecord.on, true) to await assert.equal(loanrecord.on(), true) and it should work. Or you might need to await for on() function's value to be returned before comparing.

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.