简体   繁体   中英

Unit Testing Register User Method with Jasmine in Meteor

Hey I'm trying to unit test a method but I'm getting an error "Error: createUser() method does not exist" when I spyOn Accounts.createUser, however when I spyOn Meteor.user, I don't have any problem, here's what I have so far.

server/methods/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;
});

tests/jasmine/server/unit/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: "john.smith@domain.com",
      password: "abcd1234"
    });

    expect(Accounts.createUser).toHaveBeenCalledWith({
      email: "john.smith@domain.com",
      password: "abcd1234"
    });
  });
});

Testing Jasmine unit test runs outside the Meteor context. This means that your test code is fast, isolated, and only testing the things you want them to. But code within your app that expects Meteor to be there won't run properly.

Jasmine fixe it by creating 'stubs'

beforeEach(function () {
  MeteorStubs.install();
});

afterEach(function () {
  MeteorStubs.uninstall();
});

This is done automatically for server unit tests. You need to do it yourself for your client tests if you want to write unit tests that run in the browser.

If we look at they example mock service https://github.com/alanning/meteor-stubs/blob/master/index.js we can see that ' createUser() ' is missing

stubFactories.Accounts = function () {
var Meteor = stubFactories.Meteor();

 return {
   emailTemplates: { enrollAccount: emptyFn },
   config: emptyFn,
   urls: {},
   registerLoginHandler: emptyFn,
   onCreateUser: emptyFn,
   loginServiceConfiguration: new Meteor.Collection('loginserviceconfiguration'),
   validateNewUser: emptyFn
 };

};

unlike Meteor.user is define . https://github.com/alanning/meteor-stubs/blob/master/index.js#L264

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM