嘿我想单元测试一个方法,但我得到一个错误“错误:createUser()方法不存在”当我spyOn Accounts.createUser,但是当我spyOn Meteor.user,我没有任何问题,这是我到目前为止。单元测试注册用户方法与流星茉莉
服务器/方法/ user.js的
Meteor.methods({
'registerUser' : function (options) {
if(Meteor.user())
throw new Meteor.Error(403, "Account has already been registered, log out to create a new account");
if(options.password.length < 8)
throw new Meteor.Error(403, "Password must have at least 8 characters");
var id = Accounts.createUser(options);
if(options.type === "b") Roles.addUsersToRoles(id, 'user-b');
else Roles.addUsersToRoles(id, 'user-c');
return 0;
}
});
Accounts.validateNewUser(function (user) {
if (user.emails[0].address && user.emails[0].address.length >= 5)
return true;
throw new Meteor.Error(403, "Invalid email address");
});
Accounts.onCreateUser(function(options, user) {
if(options.type === "b"){
var key = RegKey.findOne({ key: options.key, valid: true });
if(key) RegKey.update({ _id: key._id },{ valid: false });
else throw new Meteor.Error(403, "Invalid Code");
}
return user;
});
测试/茉莉/服务器/单元/ user.js的
"use strict";
describe("User", function() {
it("should be able to register with valid email and password", function() {
spyOn(Accounts, "createUser").and.returnValue("id");
Meteor.methodMap.registerUser({
email: "[email protected]",
password: "abcd1234"
});
expect(Accounts.createUser).toHaveBeenCalledWith({
email: "[email protected]",
password: "abcd1234"
});
});
});