meteor create user for integration test

627 views Asked by At

I have some server methods that requires logged in user. Simply:

'myServerMethod': function () {
  var user = Meteor.user();
  if(!user) {
    return null;
  }

  //do some action
}

Now, I want to test this method (Server Integration Test). How would I create user for my server test purpose? I am using Meteor with Velocity.

2

There are 2 answers

1
Lazov On

This is the method which tests if there are any users and if there are not it creates one:

if(Meteor.users.find().count() == 0) {
 var id = Accounts.createUser({
    email: "someemail",
    password: "somepassword",
    profile: { name: "someusername"}
  });

You could use it to test if there are users.

1
Sanjo On

I use this function (server/integration/lib/helpers.js) in those tests:

global.stubLoggedInUser = function (user) {
  spyOn(Meteor, 'user').and.returnValue(user);
  spyOn(Meteor, 'userId').and.returnValue(user._id);
  spyOn(Meteor.users, 'findOne').and.returnValue(user);
};