嗨,我想窥探下面的函数,因为函数返回一个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();更多推荐
发布评论