我正在使用 Mocha 使用测试数据库测试我的 Nodejs 应用程序。为了在每次测试之前重置数据库,我有以下代码,它运行良好:
process.env.NODE_ENV = 'test';
var knex = require('../db/knex');
describe("Add Item", function() {
beforeEach(function(done) {
knex.migrate.rollback()
.then(function() {
knex.migrate.latest()
.then(function() {
return knex.seed.run()
.then(function() {
done();
});
});
});
});
...
我已经从 mocha 切换到 mocha-casperjs 进行集成测试,现在 knex 迁移将无法运行。在每个钩子之前,我都会收到完全相同的错误消息:
undefined is not an object (evaluating 'knex.migrate.rollback')
phantomjs://platform/new-item.js:12:17
value@phantomjs://platform/mocha-casperjs.js:114:20
callFnAsync@phantomjs://platform/mocha.js:4314:12
run@phantomjs://platform/mocha.js:4266:18
next@phantomjs://platform/mocha.js:4630:13
phantomjs://platform/mocha.js:4652:9
timeslice@phantomjs://platform/mocha.js:12620:27