存根不返回值

时间:2016-08-01 07:33:50

标签: node.js unit-testing mocha sinon chai

我正在使用Mocha作为测试运行员,Chai用于断言和Sinon。 我在使用sinon时遇到问题,我有以下函数要在PrestigeQuoteService.js文件中测试

  find: function (criteria) {
    return PrestigeQuote.find(criteria)
      .populate('client')
      .populate('vehicle')
      .populate('quoteLogs');
  },

这是我的测试用例

  describe('find()', function () {
    describe('prestige quote found', function () {
      before(function () {
        sandbox = sinon.sandbox.create();
        var mockChain = {
          populate: function () {
            return this;
          }
        };
        sandbox
          .stub(PrestigeQuote, 'find').returns(mockChain);
      });

      it('should return a particular quote', function (done) {
        PrestigeQuoteService.find({id: 1}, function (err, result) {
          result.should.exist;
          done();
        });
      });

      after(function () {
        sandbox.restore();
      });
    });
  });

然而我得到了这个错误,即使我已经完成了()并且应该默认返回值。

 Error: timeout of 10000ms exceeded. Ensure the done() callback is being called in this test.

2 个答案:

答案 0 :(得分:0)

在it()函数中使用time out。

describe('find()', function () {
    describe('prestige quote found', function () {
      before(function () {
        sandbox = sinon.sandbox.create();
        var mockChain = {
          populate: function () {
            return this;
          }
        };
        sandbox
          .stub(PrestigeQuote, 'find').returns(mockChain);
      });

      it('should return a particular quote', function (done) {
         this.timeout(50000);
        PrestigeQuoteService.find({id: 1}, function (err, result) {
          result.should.exist;
          done();
        });
      });

      after(function () {
        sandbox.restore();
      });
    });
  });

答案 1 :(得分:0)

我通过在mockChain中添加一个返回来解决它

 describe('prestige quote found', function () {
      before(function () {
        sandbox = sinon.sandbox.create();
        var err = null;
        var mockChain = {
          populate: function () {
            return this;
          },
          return:function () {
            return {};
          }
        };
        sandbox
          .stub(PrestigeQuote, 'find').returns(mockChain);
      });

      it('should return a particular  prestige quote', function (done) {
       PrestigeQuoteService.find({id: 1}, function (result) {
          result.should.exist;
        });
        done();
      });

      after(function () {
        sandbox.restore();
      });
    });