【发布时间】:2015-04-12 16:52:17
【问题描述】:
我使用使用 MongoDB 的 express API 的 Supertest 进行了 mocha 测试。 MongoDB 正在运行,但我目前有 Supertest 要求并使用 express API,而不是单独启动它(我更喜欢这种方法):
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 可以代表我启动 express,我希望从这种方法开始。
【问题讨论】:
标签: node.js mocha.js supertest