【问题标题】:unit testing with sails.js using sequelize.js使用 sequelize.js 对sails.js 进行单元测试
【发布时间】:2015-08-19 09:41:57
【问题描述】:

我用 waterline(defaultI orm) 写了一些 unit test in sails 工作正常,但是当我尝试使用 sequelize orm 时出现错误。 我正在使用以下内容:

  1. 续集orm
  2. 帆-续集-挂钩
  3. 摩卡和柴
  4. 超级测试
  5. “pg-hstore”:“^2.3.2”
  6. “pg”:“^4.4.1”

我的文件夹结构是:

  ./myApp
    ├── api
    ├── assets
    ├── ...
    ├── test
    │  ├── unit
    │  │  ├── controllers
    │  │  │  └── UsersController.test.js
    │  │  ├── models
    │  │  │  └── Users.test.js
    │  │  └── ...
    │  ├── fixtures
    │  ├── ...
    │  ├── bootstrap.test.js
    │  └── mocha.opts
    └── views

我的 bootstrap.test.js 文件是:

  var Sails = require('sails'),sails;
  before(function(done) {
    this.timeout(5000);
    Sails.lift({
    }, function(err, server) {
      sails = server;
      if (err) return done(err);
      done(err, sails);
    });
  });
  after(function(done) {
    Sails.lower(done);
  });

我的连接/配置文件是:

    somePostgresqlServer: {
      user: 'postgres',
      password: 'mypassword',
      database: 'postgres',
      dialect: 'postgres',
      options: {
          dialect: 'postgres',
          host   : 'localhost',
          port   : 5432,
          logging: true
      }
    }

并且在 config/model.js 文件中是:

  connection:"somePostgresqlServer"

我的 .sailsrc 是:

  "hooks": {
      "blueprints": false,
      "orm": false,
      "pubsub": false
    }

我在 User.test.js 中写了一些测试
当我运行 mocha test/bootstrap.test.js test/unit/**/*.test.js

我收到了错误:

  error: In model (user), invalid connection :: { user: 'postgres',
    password: 'mypassword',
    database: 'postgres',
    dialect: 'postgres',
    options: 
     { dialect: 'postgres',
       host: 'localhost',
       port: 5432,
       logging: [Function: _writeLogToConsole] } }
  error: Must contain an `adapter` key referencing the adapter to use.
  npm ERR! Test failed.  See above for more details.

我做错了什么任何想法。

【问题讨论】:

  • 您是否禁用了.sailsrc 中的ormpubsub 挂钩已显示here
  • 是的,我做到了。 @AlexisN-o
  • 没有 sequelize.js(带 wateline)的单元测试工作正常,但是当我使用 sequelize 时,我得到了错误。

标签: postgresql unit-testing sails.js sequelize.js waterline


【解决方案1】:

Alexis N-o 的问题其实是一个很好的提示。

如果您查看 app.js(生成的那个帆)。它会在尝试解除之前加载 rc :) 所以只需在测试引导 js 中添加相同的代码即可修复它。

    var rc;
    try {
        rc = require('rc');
    } catch (e0) {
    try {
      rc = require('sails/node_modules/rc');
    } catch (e1) {
      console.error('Could not find dependency: `rc`.');
      console.error('Your `.sailsrc` file(s) will be ignored.');
      console.error('To resolve this, run:');
      console.error('npm install rc --save');
      rc = function () { return {}; };
    }
  }
  // Start server
  sails.lift(rc('sails'));

【讨论】:

  • 在这种情况下,我如何提供额外的选项,例如带帆索的日志?
  • 没试过。第一个参数是选项。你可以尝试将你的选项添加到第一个参数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-08
  • 2020-03-07
  • 2017-11-22
  • 2010-10-14
  • 2014-02-20
  • 2020-05-01
  • 1970-01-01
相关资源
最近更新 更多