2016-08-01 43 views
1

我正在使用摩卡作爲測試運行者,Chai爲斷言和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. 

回答

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

在它內部使用超時()函數。

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

這是行不通的,我不認爲時間的問題,我認爲函數不返回的對象,我試圖在mockChain中添加返回,但仍然無效 – user1493376