chaijs / chai-as-promised

Extends Chai with assertions about promises.
MIT License
1.42k stars 112 forks source link

Solidity contracts (UINT value) - testing problem #214

Open val3ri opened 7 years ago

val3ri commented 7 years ago

Hi guys, I need some help. I am not sure, that here is the right place for this question, but I can't find any useful information in internet (google and stackexchange/overflow) and that's why I am writing to you.

I am using mocha/chai/chai-as-promised to test some blockchain smart contracts - classic Promises: should.eventually.equal() and so on. Until now everything was perfect but I am trying to check one UINT (solidity integer value) with instance.getValue().should.eventually.equal(4) (getValue() is getter from the solidity code) but I am receiving this error the result is: expected { Object (s, e, ...) } to equal 4. When I try to compare the values like this: assert.equal(instance.getMemberCount() == 4) - it works.

Can someone explain me - what is the difference between these two ways of testing the same thing? In the both ways should be a transformation of the Promise object, but in the first one - there is not...

sorccu commented 6 years ago

This is not a bug. It works as it should.

The reason why your first example doesn't work is that .equal() in chai is a strict equality check, i.e. ===. getValue() resolves with a web3.BigNumber, not a Number, so they'll never be equivalent.

The reason why the 2nd example works is that you're not doing a strict equality check. The non-strict equality check internally ends up calling .valueOf() on the BigNumber which returns a String value of '4'. Now, even though it returned a String, you're doing a non-strict check against the Number 4, which returns true.

% node
> function Foo() {}
undefined
> Foo.prototype.valueOf = () => { console.log('valueOf called'); return '4' }
[Function]
> new Foo() == 4
valueOf called
true
> new Foo() === 4
false
>

This issue can be closed.