Nó không giống như tôi biết vấn đề của bạn nhưng nó có thể giúp bạn tìm ra của bạn nếu tôi viết xuống một số snipets của các bài kiểm tra làm việc của tôi trong buồm. Tôi cũng bao gồm tệp package.json để bạn biết phiên bản của từng mô-đun.
đây là các module liên quan trong package.json:
rootproject/test/mocha.opts, điều này có lẽ những gì u cần
--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",
...
}
Tôi cũng đã thêm một bộ điều hợp mô hình khác để sử dụng cho các thử nghiệm tại 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: '[email protected]', password: 'password'})
,new res(201,
{email: '[email protected]', password: null});
UserController.create(
new req({},
{email: '[email protected]', password: 'password'})
,new res(400,
{error: true});
);
});
});
//... more guns
}
};
Như bạn có thể thấy trên gói .json Tôi đã sử dụng buồm 0.9.7, do đó có thể cần thêm thay đổi cho phiên bản 0.10.x. .eg thay vì config/adapters.js có các cấu hình config/models.js và config/connections.js được sử dụng.