如何对一个调用另一个返回promise的函数进行单元测试?

jbe*_*nal 5 javascript unit-testing node.js promise sinon

我有一个使用express 4的node.js应用程序,这是我的控制器:

var service = require('./category.service');

module.exports = {
  findAll: (request, response) => {
    service.findAll().then((categories) => {
      response.status(200).send(categories);
    }, (error) => {
      response.status(error.statusCode || 500).json(error);
    });
  }
};
Run Code Online (Sandbox Code Playgroud)

它调用我的服务,它返回一个promise.一切正常但我在尝试进行单元测试时遇到了麻烦.

基本上,我想确保根据我的服务返回的内容,我使用正确的状态代码和正文刷新响应.

因此,对于mocha和sinon,它看起来像:

it('Should call service to find all the categories', (done) => {
    // Arrange
    var expectedCategories = ['foo', 'bar'];

    var findAllStub = sandbox.stub(service, 'findAll');
    findAllStub.resolves(expectedCategories);

    var response = {
       status: () => { return response; },
       send: () => {}
    };
    sandbox.spy(response, 'status');
    sandbox.spy(response, 'send');

    // Act
    controller.findAll({}, response);

    // Assert
    expect(findAllStub.called).to.be.ok;
    expect(findAllStub.callCount).to.equal(1);
    expect(response.status).to.be.calledWith(200); // not working
    expect(response.send).to.be.called; // not working
    done();
});
Run Code Online (Sandbox Code Playgroud)

当我测试的函数返回一个promise时,我已经测试了我的类似场景,因为我可以在当时挂钩我的断言.

我也试图用一个Promise包装controller.findAll并从response.send解析它,但它既不起作用.

Joh*_*erz 6

您应该将断言部分移动到res.send方法中,以确保在断言之前完成所有异步任务:

var response = {
   status: () => { return response; },
   send: () => {
     try {
       // Assert
       expect(findAllStub.called).to.be.ok;
       expect(findAllStub.callCount).to.equal(1);
       expect(response.status).to.be.calledWith(200); // not working
       // expect(response.send).to.be.called; // not needed anymore
       done();
     } catch (err) {
       done(err);
     }
   },
};
Run Code Online (Sandbox Code Playgroud)