如何使用sinon监视内部存根返回(How to spy inside stub returns with sinon)

编程入门 行业动态 更新时间:2024-10-07 22:29:02
如何使用sinon监视内部存根返回(How to spy inside stub returns with sinon)

嗨,我想窥探下面的函数,因为函数返回一个promise,所以我需要返回promise,让其他代码继续运行。

但它从未通过测试,我做错了吗? 我怎么能正确地做到这一点? 先谢谢你。

抱歉,我可能没有清楚地描述问题, another_fn将调用object.method.origin_fn ,并且orgin_fn返回一个promise; 我想窥探origin_fn被调用。 我怎样才能做到这一点?

let spy_fn = sinon.spy(); sinon.stub(object, 'method').returns({ origin_fn(args) { spy_fn(args); return Promise.resolve(); } }); another_fn('test_args'); spy_fn.should.have.been.calledWith('test_args');

Hi I want to spy the function as below, and because the function returns a promise, so I need to return the promise let other code continue running.

But it never passes the test, am I doing it wrong? how can I do this correctly? Thank you in advance.

Sorry I might not describe the problem clearly, another_fn will invoke object.method.origin_fn, and the orgin_fn returns a promise; I want to spy the origin_fn is get called. How can I do this?

let spy_fn = sinon.spy(); sinon.stub(object, 'method').returns({ origin_fn(args) { spy_fn(args); return Promise.resolve(); } }); another_fn('test_args'); spy_fn.should.have.been.calledWith('test_args');

最满意答案

如果您只想确保使用正确的参数调用origin_fn则可以使用以下内容:

let spy = sinon.stub(object, 'method').returns({ origin_fn(arg) { arg.should.equal('test_args'); } }) let fn = function() { another_fn('test_args'); }; fn.should.not.throw();

If you only want to make sure that origin_fn is called with the correct argument, you can use something like this:

let spy = sinon.stub(object, 'method').returns({ origin_fn(arg) { arg.should.equal('test_args'); } }) let fn = function() { another_fn('test_args'); }; fn.should.not.throw();

更多推荐

本文发布于:2023-04-29 07:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335589.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:存根   如何使用   sinon   returns   spy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!