【问题标题】:Unit test mongoose promises with sinon单元测试猫鼬承诺与 sinon
【发布时间】:2016-05-07 01:24:10
【问题描述】:

我正在尝试对使用 Promise 的 mongoose 对象进行单元测试。我已经编写了下面的测试,它可以工作,但它并不完整。我不知道如何测试是否调用了“then”或“catch”方法。

我如何使用 spy 来检查当我解决 promise 时是否调用了 'then' 方法?

测试方法

export function create(req, res) {
  User
    .createAsync(req.body)
    .then(handleCreate(res, req.originalUrl))
    .catch(handleError(res));
}

单元测试

it('should do something', () => {
  const req = {
    body: 45,
  };

  const res = {};

  const mockRole = sandbox.mock(Role).expects('createAsync').once().withArgs(45)
    .returns(Promise.resolve());

  controller.create(req, res);
});

更新我使用的解决方案(2016 年 5 月 6 日)

感谢@ReedD 帮助我朝着正确的方向前进

虽然这“有效”,但我觉得我在测试 Promise 的功能而不是我的代码。

it('should call create with args and resolve the promise', () => {
  const createSpy = sinon.spy();
  const errorSpy = sinon.spy();

  sandbox.stub(responses, 'responseForCreate').returns(createSpy);
  sandbox.stub(responses, 'handleError').returns(errorSpy);

  sandbox.mock(Role).expects('createAsync').once().withArgs(45)
    .returns(Promise.resolve());

  return controller.create(req, res).then(() => {
    expect(createSpy.calledOnce).to.be.equal(true);
    expect(errorSpy.calledOnce).to.be.equal(false);
  });
});

【问题讨论】:

    标签: node.js unit-testing mongoose bluebird es6-promise


    【解决方案1】:

    您可以将handleCreate 和handleError 添加到module.exports,然后制作存根或间谍。下面是我认为你正在尝试做的一个例子。我还假设您使用的是 sinon/chai。

    http://ricostacruz.com/cheatsheets/sinon-chai.html

    // controller.js
    
    module.exports = {
        handleCreate: function () {
            // ..code
        },
        handleError: function () {
            // ..code
        },
        create: function (req, res) {
            User
                .createAsync(req.body)
                .then(this.handleCreate(res, req.originalUrl))
                .catch(this.handleError(res));
        }
    };
    
    
    
    // test/test.js
    
    var controller = require('../controller');
    
    it('should do something', function (done) {
    
        var handleCreate = sandbox.spy(controller, 'handleCreate');
        var handleError  = sandbox.spy(controller, 'handleError');
        var mockRole     = sandbox
            .mock(Role)
            .expects('createAsync')
            .once().withArgs(45)
            .returns(Promise.resolve());
    
    
        var req = {
            body: 45,
        };
    
        var res = {
            send: function () {
                expect(handleCreate).to.be.calledOnce;
                expect(handleError).to.not.be.called;
                done();
            }
        };
    
        controller.create(req, res);
    });
    

    【讨论】:

    • 谢谢。这让我走上了我最终使用的解决方案的正确轨道。最后,我觉得我只是在对 Promises 的功能进行单元测试:D
    • 没错,这个单元测试只确保你的create函数工作正常。您应该编写更多的单元测试以独立确保handleCreate 和handleError 也能正常工作。
    猜你喜欢
    • 2016-05-19
    • 1970-01-01
    • 2017-04-26
    • 2018-03-25
    • 2015-05-12
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    相关资源
    最近更新 更多