0
我想爲我在Sails.js中創建的模型編寫一些測試。該模型如下圖所示:Sailjs測試模型
/**
* Users
*
* @module :: Model
* @description :: A short summary of how this model works and what it represents.
* @docs :: http://sailsjs.org/#!documentation/models
*/
var User = {
/**
* Attributes list for the user
* @type {Object}
*/
attributes: {
password:{
type: 'string',
alphanumeric: true,
required: true,
minLength: 6
},
firstName: {
type: 'string',
maxLength: 50,
minLength: 5,
required: true
},
lastName:{
type: 'string',
maxLength: 50,
minLength: 5,
required: true
},
email: {
type: 'email',
unique: true,
required: true
}
},
/**
* Function that hashes a password
* @param {Object} attrs - user attributes list
* @param {Function} next [description]
* @return {[type]} [description]
*/
beforeCreate: function(attrs, next) {
var bcrypt = require('bcrypt');
bcrypt.genSalt(10, function(err, salt) {
if (err) return next(err);
bcrypt.hash(attrs.password, salt, function(err, hash) {
if (err) return next(err);
attrs.password = hash;
next();
});
});
}
};
module.exports = User;
我已經編寫了測試如下所示:
var assert = require('assert')
, Sails = require('sails')
, barrels = require('barrels')
, fixtures;
// Global before hook
before(function (done) {
// Lift Sails with test database
Sails.lift({
log: {
level: 'error'
},
adapters: {
default: 'mongo'
}
}, 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();
sails.lower(done);
});
// User test
describe('Users', function(done) {
it("should be able to create", function(done) {
Users.create({firstName: "johnny", lastName: "BeGood", email: "[email protected]mple.com", password: "validpassword123"}, function(err, user) {
assert.notEqual(user, undefined);
done();
});
});
it("should be able to destroy", function(done) {
Users.destroy({email: "[email protected]"}, function(err) {
assert.equal(true, true);
});
});
});
不過,我永遠只能得到第一個測試通過,我收到如下所示的輸出:
我很新在節點/帆寫作測試。任何人都可以指出我在這種情況下做錯了什麼。我利用以下要點 https://gist.github.com/joukokar/57b14e034e41893407f0
謝謝你,我會試試這個。我把真實,真實只是爲了看看它是不是給我一個暫停:) – kushaldsouza