0
votes

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');
1

1 Answers

0
votes

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();