最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to test a stub returning a promise in an async test? - Stack Overflow

programmeradmin3浏览0评论

How can I test this in a async manner?

it('Should test something.', function (done) {

    var req = someRequest,
        mock = sinon.mock(response),
        stub = sinon.stub(someObject, 'method');

     // returns a promise
     stub.withArgs('foo').returns(Q.resolve(5));

     mock.expects('bar').once().withArgs(200);

     request(req, response);

     mock.verify();

});

And here is the method to test.

var request = function (req, response) {

    ...

    someObject.method(someParameter)
        .then(function () {
            res.send(200);
        })
        .fail(function () {
            res.send(500);
        });

};

As you can see I am using node.js, Q (for the promise), sinon for mocking and stubbing and mocha as the test environment. The test above fails because of the async behaviour from the request method and I don't know when to call done() in the test.

How can I test this in a async manner?

it('Should test something.', function (done) {

    var req = someRequest,
        mock = sinon.mock(response),
        stub = sinon.stub(someObject, 'method');

     // returns a promise
     stub.withArgs('foo').returns(Q.resolve(5));

     mock.expects('bar').once().withArgs(200);

     request(req, response);

     mock.verify();

});

And here is the method to test.

var request = function (req, response) {

    ...

    someObject.method(someParameter)
        .then(function () {
            res.send(200);
        })
        .fail(function () {
            res.send(500);
        });

};

As you can see I am using node.js, Q (for the promise), sinon for mocking and stubbing and mocha as the test environment. The test above fails because of the async behaviour from the request method and I don't know when to call done() in the test.

Share Improve this question asked Apr 15, 2013 at 12:56 StefanStefan 7491 gold badge11 silver badges25 bronze badges 2
  • Easiest way i found in working with async calls, when you want to use the response, was to break the function in 2 at the point where the response is needed and call the second part when the response is received. – cosmin.danisor Commented Apr 15, 2013 at 13:50
  • you need to choose an answer, not just upvote. – oligofren Commented Jun 16, 2017 at 12:34
Add a ment  | 

2 Answers 2

Reset to default 7

You need to call done once all the async operations have finished. When do you think that would be? How would you normally wait until a request is finished?

it('Should test something.', function (done) {

   var req = someRequest,
       mock = sinon.mock(response),
       stub = sinon.stub(someObject, 'method');

    // returns a promise
    stub.withArgs('foo').returns(Q.resolve(5));

    mock.expects('bar').once().withArgs(200);

    request(req, response).then(function(){
       mock.verify();
       done();
    });

});

It might also be a good idea to mark your test as failing in an errorcallback attached to the request promise.

Working solution in Typescript:

 var returnPromise = myService.method()
    returnPromise.done(() => {
        mock.verify()
    })
发布评论

评论列表(0)

  1. 暂无评论