I need to do some expectation in Jasmine, like:
let realValue = callSomeMethod();
let expected = [{
total: 33,
saved: 1.65
}];
expect(realValue).toEqual(expected);
But it fails, and the message is:
Expect [ Object({ total: 33, saved: 1.6500000000000001 })]
to equal [Object({ total: 33, saved: 1.65 })].
How can I do the right checking?
The toBeCloseTo
matcher is for precision math comparison:
expect(1.6500000000000001).toBeCloseTo(1.65, 2);
expect(1.6500000000000001).toBeCloseTo(1.65, 15);
expect(1.6500000000000001).not.toBeCloseTo(1.65, 16);
source: Jasmine matchers
In the case some properties are floats and some are not:
let realValue = callSomeMethod();
let expectedFloat = {
saved: 1.65,
otherFloat: 6.44
}
let expectedOther = {
firstName: 'Paul',
total: 33,
};
let expected = {
...expectedFloat,
...expectedOther
}
let floatNames = Object.getOwnPropertyNames(expectedFloat);
const testFloatProperty = (name) => () => {
expect(realValue[name]).toBeCloseTo(expectedFloat[name], 3)
}
for (let i in floatNames) {
it('should be close to expected ' + floatNames[i], testFloatProperty(floatNames[i]));
}
it('should contain other expected props', function() {
expect(realValue).toEqual(jasmine.objectContaining(expectedOther))
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.