【问题标题】:Stub doesn't return a value存根不返回值
【发布时间】:2016-08-01 07:33:50
【问题描述】:

我使用 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();
      });
    });
  });

但我得到了这个错误,即使我认为我已经 done() 并且应该默认返回值。

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

【问题讨论】:

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


    【解决方案1】:

    在 it() 函数中使用超时。

    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();
          });
        });
      });
    

    【讨论】:

    • 不行,我觉得不是时间问题,我觉得函数没有返回对象,我尝试在mockChain中加return还是不行
    【解决方案2】:

    我通过在 mockChain 中添加 return 解决了这个问题

     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();
          });
        });
    

    【讨论】:

      猜你喜欢
      • 2012-10-28
      • 1970-01-01
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多