ser*_*gpa 5 unit-testing sinon angular
我正在尝试测试我的 Sinon 测试中的间谍是否被一个确切的对象调用:没有丢失的属性,没有附加的属性,也没有更改的属性。
我有这个:
assert( viewer.entities.add.calledWith( completeEntityObject ) );
Run Code Online (Sandbox Code Playgroud)
但是如果我从 中省略某些属性completeEntityObject,则测试成功。我希望它失败。我想要一个深刻的比较。
我曾尝试查看这些sinon.match方法,但是,尽管有针对数组深度相等的测试,但没有针对对象的此类测试。我怎样才能做到这一点?
对于任何正在寻找如何断言您的 sinon 间谍是用某些参数调用的人,这里有一个简单的例子。
const spy = sinon.spy();
// If spy is ever called in your code with some arguments, like so:
spy({ hello: 'world'});
// You can assert for it in your tests like this:
expect(spy.getCall(0).calledWith(sinon.match({ hello: 'world' }))).to.be.true;
// Breaking this down a bit:
// spy.getCall(0) gives us the first time our spy function was called (because it can be called more than once)
// Bonus: While debugging, you can use spy.getCalls()[0].args[0] to get the arguments that your spy function was called with.
Run Code Online (Sandbox Code Playgroud)
我希望这是有帮助的 - 这是我的第一个 SO 回复,所以如果我违反规则或没有详细解释,请原谅我。
ser*_*gpa -3
我找到了解决方案。我用过这个:
sinon.assert.calledWith( addStub, sinon.match( cesiumPartialEntityObject ) );
Run Code Online (Sandbox Code Playgroud)
如果缺少或有附加属性,此操作将失败。
| 归档时间: |
|
| 查看次数: |
2545 次 |
| 最近记录: |