单元测试使用Mocha返回promise的多个异步调用

时间:2016-05-24 15:00:07

标签: javascript node.js mocha sinon chai

我试图了解如何最好地对我的异步CommonJS模块进行单元测试。在处理多个链式承诺时,我很难理解最佳实践。

让我们假设我定义了以下模块:

module.exports = function(api, logger) {
    return api.get('/foo')
        .then(res => {
            return api.post('/bar/' + res.id)
        })
        .then(res => {
            logger.log(res)
        })
        .catch(err => {
            logger.error(err)
        })
}

我有以下规范文件试图测试正确的调用。

var module = require('./module')
describe('module()', function() {
    var api;
    var logger;
    var getStub;
    var postStub;
    beforeEach(function() {
        getStub = sinon.stub();
        postStub = sinon.stub();
        api = {
            get: getStub.resolves({id: '123'),
            post: postStub.resolves()
        }
        logger = {
            log: sinon.spy(),
            error: sinon.spy()
        }
    })
    afterEach(function() {
        getStub.restore();
        postStub.restore();
    });
    it('should call get and post', function(done) {
        module(api, logger) // System under test
        expect(getStub).to.have.been.calledWith('/foo')
        expect(postStub).to.have.been.calledWith('/bar/123')
        done()
    })
})

这不起作用。第一个断言正确传递,但第二个断言失败,大概是承诺在执行时没有得到解决。

我可以使用process.nextTick或setTimeout解决这个问题,但我想看看其他人如何更优雅地解决这个问题。

我尝试将运气添加到混合物中,运气不佳。我目前的设置包括,sinon,chai,sinon-as-promise和sinon-chai。

谢谢

1 个答案:

答案 0 :(得分:1)

你应该使用module()返回一个promise的事实,这样你就可以在链中添加另一个.then()来断言参数(因为那时前面有.then()个步骤已被调用,包括调用api.post())。

由于Mocha支持承诺,您可以返回由此产生的承诺,而不必处理done

it('should call get and post', function() {
  return module(api, logger).then(() => {
    expect(getStub).to.have.been.calledWith('/foo')
    expect(postStub).to.have.been.calledWith('/bar/123')
  });
})
相关问题