【发布时间】:2017-05-08 14:42:10
【问题描述】:
我正在用 sinon.js 测试一个方法调用,它需要一个对象数组,如下所示:
let f = function(arr) {}
f([{foo: "bar"}, {baz: "quux"}];
我想在我的测试中使用sinon matchers 对f 的调用内容执行断言。
我在f 上有一个间谍,叫fSpy,我已经可以了
sinon.assert.calledOnce(fSpy);
sinon.assert.calledWith(fSpy, sinon.match.array);
但是,如果我测试类似的东西
sinon.assert.calledWith(fSpy, sinon.match.array.contains(sinon.match.has("foo")));
测试失败。
我猜这是因为匹配器的参数不能是匹配器本身,那么测试这个的正确方法是什么?
【问题讨论】:
-
是的,但是 sinon 匹配器只有在作为参数传递给 sinon 函数 AFAIK 时才有效,因此将其传递给
contains不起作用
标签: javascript unit-testing sinon