Is it possible to test the value is contained within certain array with Chai assertion library?
Example:
var myObject = {
a : 1,
b : 2,
c : 3
};
var myValue = 2;
I need to do something like this (however it is not working):
expect(myObject).values.to.contain(myValue);
//Error: Cannot read property 'to' of undefined
expect(myObject).to.contain(myValue);
//Error: Object #<Object> has no method 'indexOf'
How can I test this?
Important Concept: By default, all assertions in Chai are performing a strict equality comparison. Thus, asserting that an array of objects has a member object will cause those two objects to be compared strictly. Adding in the deep flag signals to the assertion to instead use deep equality for the comparison.
Note expect and should uses chainable language to construct assertions, but they differ in the way an assertion is initially constructed. In the case of should , there are also some caveats and additional tools to overcome the caveats. var expect = require('chai').
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.
expect , should = chai. should(); The expect interface provides a function as a starting point for chaining your language assertions. It works on node. js and in all browsers.
Alternatively if you want to check the value along with the property you can do
expect(myObject).to.have.property('b', myValue);
You won't need the plugin for this check.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With