在 Javascript 测试 (chai) 中断言对象相等 [英] Asserting object equality in Javascript test (chai)
本文介绍了在 Javascript 测试 (chai) 中断言对象相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在 JavaScript 单元测试中断言两点之间的相等性:
I need to assert equality between two points in my JavaScript unit tests:
var pnt1 = {x: 2, y: 3};
和
var pnt2 = {x: 2, y: 3};
当我这样做
assert.equal(pnt1, pnt2);
它说点是不同的.我可以从检查中排除对象是不同实例的事实吗(因此实际上它们不相等")?
It says the points are different. Can I exclude from the check the fact that the objects are different instances (so in fact they are "not equal")?
我想避免创建一个断言列表,一个用于每个要测试的字段(在本例中为 .x
和 .y
)
I'd like to avoid creating a list of assert, one for each field to test (in this case .x
and .y
)
推荐答案
代替 .equal
,使用 .deepEqual
:
Instead of .equal
, use .deepEqual
:
assert.deepEqual(pnt1, pnt2);
这将执行深度比较,而不是简单地检查相等性.
This will perform a deep comparison instead of simply checking for equality.
这篇关于在 Javascript 测试 (chai) 中断言对象相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文