对于我的sails应用程序,我正在使用以下代码对用户模型进行单元测试,但收到错误消息:
'TypeError:对象#没有方法'create'
var User = require('../../api/models/User');
var Sails = require('sails');
console.log(User);
describe("User Model:", function() {
// create a variable to hold the instantiated sails server
var app;
// Global before hook
before(function(done) {
// Lift Sails and start the server
Sails.lift({
log: {
level: 'error'
},
}, function(err, sails) {
app = sails;
done(err, sails);
});
});
// Global after hook
after(function(done) {
app.lower(done);
});
describe("Password encryption", function() {
describe("for a new user", function() {
var user;
before(function (cb) {
var userData = {
email: "[email protected]",
password: "test_password",
passwordConfirmation: "test_password"
};
User.create(userData, function (err, newUser) {
if (err) return cb(err);
user = newUser;
cb();
});
});
it("must encrypt the password", function() {
user.must.have.property('encryptedPassword');
user.must.not.have.property('password');
user.must.not.have.property('passwordConfirmation');
});
after(function (cb){
user.destroy(function (err) {
cb(err);
});
});
});
在我看来,帆已经正确举起了,导致create方法不可用的问题是什么?
删除第一行:
var User = require('../../api/models/User');
启航Sails应用后,您将自动获得模型,例如,请参见此处。
在您的情况下,您的第一行将覆盖User
Sails.js否则将构建的模型,这就是为什么即使您有一个对象也不是Waterline模型的原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句