我需要在Jasmine中做一些期待,比如:
let realValue = callSomeMethod(); let expected = [{ total: 33,saved: 1.65 }]; expect(realValue).toEqual(expected);
但它失败了,消息是:
Expect [ Object({ total: 33,saved: 1.6500000000000001 })] to equal [Object({ total: 33,saved: 1.65 })].
我怎样才能做正确的检查?
解决方法
toBeCloseTo匹配器用于精确数学比较:
expect(1.6500000000000001).toBeCloseTo(1.65,2); expect(1.6500000000000001).toBeCloseTo(1.65,15); expect(1.6500000000000001).not.toBeCloseTo(1.65,16);