I am testing at the moment some smart contracts with truffle(mocha/chai/chai-as-promised) and have the following problem:

I have a getter, and this getter delivers an UINT value, then when I try to test the value with the instance.getMemberCount().should.eventually.equal(4) the result is: expected { Object (s, e, ...) } to equal 4.

Interesting fact is that when I try to compare the values directly with ‘==’ there is no problem.. (for example: assert.equal(instance.getMemberCount() == 4))

Has Mocha/Chai problems transforming the uint values?

  • getMemberCount().toNumber() ? maybe – Виталий Швецов Aug 11 '17 at 3:55
  • @ВиталийШвецов - there is no toNumber() in JavaScript or I am wrong? I have tried with Number(), parseInt() - but the result is NaN, because we have 'object Promise' and I am still stuck .. – val3ri Aug 11 '17 at 7:23
  • @val3ri Web3 has a BigNumber object which has a toNumber. ethereumbuilders.gitbooks.io/guide/content/en/… see "web3.toBigNumber" – ABCD.ca Jun 30 '18 at 21:30

You can use .valueOf() to get the return value from the getter function.

I've used this in one of the test cases I wrote sometime back.

You can take a look at this for reference.

Hope this helps.

  • Thanks for the github repository link I will search for answers there! With .valueOf() the result is: [object Promise]. And after printing the result is: { [String: '4'] s: 1, e: 0, c: [ 4 ] }. JavaScript is converting somehow this object and the comparison with "==" works. – val3ri Aug 14 '17 at 10:08

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.