Sails js 运行测试

6
我正在尝试运行我的Sails单元测试(使用mocha和istanbul)。
当运行时,...
grunt test

我遇到了错误。
  1) "before all" hook
  2) "after all" hook

  0 passing (5s)
  2 failing

  1)  "before all" hook:
     Error: timeout of 2000ms exceeded
      at null.<anonymous> (/vagrant/node_modules/mocha/lib/runnable.js:157:19)
      at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

  2)  "after all" hook:
     ReferenceError: sails is not defined

安装似乎找不到我的Sails...但是正在进行。
which sails

我明白了。
/usr/local/node/node-default/bin/sails

“and running sails lift works fine” 的意思是“运行 sails lift 没问题”。
“Here is the mocha tests file in my project”的意思是“这是我的项目中的 mocha 测试文件”。
//boostrap.test.js
var Sails = require('sails');

before(function(done) {
  Sails.lift({
    // configuration for testing purposes
  }, function(err, sails) {
    if (err) return done(err);
    // here you can load fixtures, etc.
    done(err, sails);
  });
});

after(function(done) {
  // here you can clear fixtures, etc.
  sails.lower(done);
});
4个回答

4

首先,您有一个拼写错误问题,您的文本中有:

var Sails = require('sails');

但是你尝试使用

sails.lower(done);

替代

标签

Sails.lower(done);

接下来,您需要定义一个项目范围的“迁移”设置 (http://sailsjs.org/#/documentation/concepts/ORM/model-settings.html?q=migrate)。您只需编辑config/models.js文件即可。您可以简单地取消以下行的注释:

//migrate: 'alter'

祝愉快 :)


2

我还没有看到您的测试用例实际代码,所以这里只列举了一些可能性。

如果您异步运行测试,我认为您需要在mocha中设置timeout属性为更大的数字。我编写了三个非常简单的测试用例,但我总是遇到您描述的相同问题。我尝试了10000ms、20000ms和30000ms。当我将它增加到90000ms时,所有的测试用例都通过了。所以我认为这是因为Sails在测试开始之前确实需要一些时间来启动。

另一个想法是,您是否在环境配置文件中设置了'migrate'属性?如果没有,sails lift命令将等待您的输入来选择“drop”、“alter”或“safe”,这也会导致测试超时问题。


1

我并不知道您的具体问题,但如果我将一些在sails中工作测试的片段写下来,可能会帮助您找到自己的问题。我同时也包含了package.json文件,以便您了解每个模块的版本。

以下是package.json中相关的模块:

rootproject/test/mocha.opts,这可能是您需要的。

--timeout 5000

rootproject/package.json

{
  ...
  "dependencies": {
    ...
    "sails": "0.9.7",
    "sails-disk": "~0.9.0",
    "sails-memory": "^0.9.1",
    "sails-mongo": "^0.9.7",
    ...
  },
  "devDependencies": {
    "mocha": "^1.20.1",
    "barrels": "^0.0.3",
    "supervisor": "^0.6.0"
  },
  "scripts": {
    "start": "node app.js",
    "debug": "node debug app.js",
    "test": "PORT=9999 NODE_ENV=test mocha -R spec -b --recursive"
  },
  "main": "app.js",
  ...
}

我还添加了另一个模型适配器供测试使用,其位于 rootproject/config/adapters.js
test: {
  module   : 'sails-memory'
},

rootproject/test/index.js

var assert = require('assert');
var Sails = require('sails');
var barrels = require('barrels');

var fixtures;

var userTest = require('./controllers/User.js');
//... other test controllers  ...

//in case you need different simulations per controller you could add a custom Response in your test controller and use them instead
var defaultCustomRequest = function(urlParams, bodyParams/*whatever else u need*/) {
  //simulates the sails request
  //create an object here, based on how u use the req object in your sails controllers
  //.eg
  return {
    params: urlParams,
    body: bodyParams
  };
}

//in case you need different simulations per controller or per method you could add multiple custom Responses in your test controller and use them instead
var defaultCustomResponse = function(expectedCode, dataExpecting/*whatever else u need*/) {
  //simulates the sails res which I was using like this: res.status(httpCode).json({somedata})
  //use the assert function here to validate 
  //.eg
  status: function (responseCode){
    assert(expectedCode === responseCode, 'Expected status is ' + expectedCode + ' but ' + responseCode + ' was returned.');
    return this;
  },
  json: function (responseData){
    //assert your responseData with your expectedData
    return this;
  }
  return this;
},
}

before(function (done) {
  // Lift Sails with test database
  Sails.lift({
    log: {
      level: 'error'
    },
    adapters: {
      default: 'test'
    }
  }, function(err, sails) {
    if (err)
      return done(err);
    // Load fixtures
    barrels.populate(function(err) {
      done(err, sails);
    });
    // Save original objects in `fixtures` variable
    fixtures = barrels.objects;
  });
});

// Global after hook
after(function (done) {
  //console.log('fixtures loaded: ' + JSON.stringify(fixtures));
  sails.lower(done);
});

describe('User', function() { userTest.run(fixtures, customRequest, customRespose); });
//or describe('User', function() { userTest.run(fixtures); });
//... other test controllers ...

rootproject/test/controllers/User.js

var assert = require('assert');

var UserController = require('../../api/controllers/UserController.js');

module.exports = {

  run: function(fixtures, customReq, customRes) {
    describe('create', function(customReq, customRes) {
      it ('should create a few user entries', function() {
        if (!customReq) customReq = {/*custom request for user create*/};
        if (!customRes) customRes = {/*custom response for user create*/};

       //call your controllers for testing here
       //.eg
       UserController.create(
        new req({},
               {email: 'someemail@gmail.com', password: 'password'})
        ,new res(201,
                {email: 'someemail@gmail.com', password: null});
       UserController.create(
        new req({},
               {email: 'someemail@gmail.com', password: 'password'})
        ,new res(400,
                {error: true});
    );

      });
    });

    //... more guns
  }
};

正如您在package.json中看到的,我使用的是Sails 0.9.7,因此可能需要针对0.10.x版本进行额外的更改。例如,config/adapters.js被替换为config/models.jsconfig/connections.js

0

对于其中一个错误,被接受的答案是正确的。

对于超时错误,只需添加以下内容:

this.timeout(5000);

在下一行

before(function(done) {

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接