我有一個使用Supertest的使用MongoDB的快速API的摩卡測試。 MongoDB是跑步,但我目前有Supertest要求和使用Express API而不是單獨啓動它(我更喜歡這種方法):在API初始化之前持有Supertest
var request = require('supertest');
var chai = require('chai');
var api = require('../../server/api.js');
chai.should();
describe("/api/lists", function() {
it("should be loaded", function() {
api.should.exist;
});
it("should respond with status 200 ", function(done) {
request(api)
.get('/api/lists')
.expect(200, done);
});
});
當測試運行時,它失敗:
TypeError: Cannot call method 'collection' of undefined
at app.get.listId (/my/path/api.js:63:5)
我懷疑supertest是在MongoDB連接建立之前在我的API上運行測試。在我的API完全初始化之前,什麼才能讓它延續下去?
我想如果我在啓動express之後通過Grunt進行測試,它會很好,但是由於Supertest可以代表我開始表達,所以我希望能夠以這種方式開始。