嘿我想單元測試一個方法,但我得到一個錯誤「錯誤: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"
});
});
});