0

我正在使用 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
4

1 回答 1

0

我很确定 webpack 构建中不包含迁移功能。如果您访问http://knexjs.org/打开调试控制台并检查不同的客户端,例如mysql.migrate您会看到根本没有声明任何函数。

实际上,如果您显式加载 webpack 构建而不是节点库,您也可以使用节点检查它。

// load webpack build instead of node build...
let knex = require('knex/build/knex')({client : 'pg'});
console.log(knex.migrate);
// outputs: {}

所以......问题是你为什么要在 PhantomJS 浏览器而不是 node.js 上运行测试?

client.migrate 为空

于 2016-07-01T12:41:34.763 回答