航行js运行testing

我试图运行我的帆unit testing(使用摩卡和伊斯坦布尔)

运行时

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 

设置似乎没有find我的风帆,但是在做

 which sails 

我明白了

 /usr/local/node/node-default/bin/sails 

并运行sails lift工作正常

这是我的项目中的摩卡testing文件

 //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); }); 

首先,你有一个错字问题,你有:

 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' 

请享用 :)

我没有看到你的testing用例的实际代码,所以我只列出了几个可能的情况。

如果你asynchronous运行你的testing,我认为你需要在摩卡中设置超时属性,使其大一些。 我写了三个非常简单的testing用例,但是我总是遇到同样的问题。 我试过10000ms,20000ms和30000ms。 当我把它增加到90000ms时,所有的testing用例都通过了。 所以我想这是因为帆船在testing开始之前真的需要一些时间来提升。

另一个想法是,你有没有在你的环境configuration文件中设置“迁移”属性? 如果不是的话,升船命令会等待你的input,在继续之前select“drop”,“alter”或“safe”,这也会造成testing超时的问题。

它不是像我知道你的问题,但它可以帮助你找出你的,如果我写下我的工作testing在帆船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中添加了另一个模型适配器供testing使用

 test: { module : 'sails-memory' }, 

rootproject /testing/ 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 /testing/控制器/ 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上看到的,我使用了0.9.7版本的sails,因此可能需要对0.10.x版本进行额外的修改。 .eg而不是config / adapters.js,这里有config / models.jsconfig / connections.js

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

对于超时错误,只需添加

 this.timeout(5000); 

上线后

 before(function(done) {