跑js运行测试

时间:2014-10-10 13:31:11

标签: node.js gruntjs sails.js mocha

我正在尝试运行我的风帆单元测试(使用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

并且正常运行sails lift

这是我项目中的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 个答案:

答案 0 :(得分: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'

享受:)

答案 1 :(得分:2)

我还没有看到测试用例的实际代码,所以我在这里列出了几种可能性。

如果异步运行测试,我认为你需要将mocha中的timeout属性设置为更大的数字。我写了三个非常简单的测试用例,但是我总是遇到和你描述的一样的问题。我试过10000ms,20000ms和30000ms。当我将其增加到90000ms时,所有测试用例都会通过。所以我认为这是因为在测试开始之前帆需要一些时间才能解除。

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

答案 2 :(得分:1)

它不像我知道你的问题,但如果我在帆中写下我的工作测试的一些snipets,它可能会帮助你找到你的问题。我也包括 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 /测试/ 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 /测试/控制器/ 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版本进行其他更改。 .eg而不是 config / adapters.js 有使用的 config / models.js config / connections.js

答案 3 :(得分:0)

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

对于超时错误,只需添加

this.timeout(5000);

之后的

before(function(done) {