gpt4 book ai didi

angular - SinonJS : calledWith() with exact object

转载 作者:行者123 更新时间:2023-12-03 23:17:17 26 4
gpt4 key购买 nike

我正在尝试测试我的 Sinon 测试中的 spy 是否被一个确切的对象调用:没有丢失的属性,没有附加的属性,也没有更改的属性。

我有这个:

assert( viewer.entities.add.calledWith( completeEntityObject ) );

但是如果我省略了 completeEntityObject 中的一些属性,测试成功。我希望它失败。我想要一个深刻的比较。

我试过查看 sinon.match方法但是,虽然有测试数组是否深度相等,但没有针对对象的测试。我怎样才能做到这一点?

最佳答案

对于任何正在寻找如何断言您的 sinon spy 是用某些参数调用的人,这里有一个简单的例子。

  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.

我希望这是有帮助的 - 这是我的第一个 SO 回复,所以如果我违反规则或没有详细解释,请原谅我。

关于angular - SinonJS : calledWith() with exact object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48118073/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com