已用对象断言调用 [英] Has been called with object assertion

查看:55
本文介绍了已用对象断言调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在监视的功能,接收对象作为参数.我需要断言该函数是用对象的某些属性调用的.

Function I'm spying on, receives object as an argument. I need to assert that the function been called with certain properties of the object.

例如:我的SUT具有:

e.g: my SUT has:

function kaboom() {

    fn({ 
        foo: 'foo', 
        bar: 'bar', 
        zap: function() { ... },
        dap: true
     });
}

在我的测试中,我可以这样做:

and in my test I can do this:

fnStub = sinon.stub();
kaboom();
expect(fnStub).to.have.been.called;

并且有效(很高兴知道 fn 被调用了).现在,我需要确保正确的对象已传递到函数中.我只关心 foo bar 属性,即我必须为参数的特定属性设置匹配项.怎么样?

and that works (it's good to know that fn's been called). Now I need to make sure that the right object has been passed into the function. I care about only foo and bar properties, i.e. I have to set match for specific properties of the argument. How?

upd:sinon.match()似乎适用于简单对象.让我们提高标准吧?

upd: sinon.match() seems to work for simple objects. Let's raise the bar, shall we?

如果我想在断言中包含 zap 函数怎么办?我该如何工作?

What if I want to include zap function into assertion? How do I make that work?

推荐答案

假设您使用的是 sinon-chai ,您可以与 sinon.match 一起使用 namedWith 来实现这一目标

Assuming you're using sinon-chai, you can use calledWith along with sinon.match to achieve this

expect(fnStub).to.have.been.calledWith(sinon.match({
  foo: 'foo',
  bar: 'bar'
}));

这篇关于已用对象断言调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆