Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking Meteor.user() #111

Closed
caio-ribeiro-pereira opened this issue Mar 22, 2014 · 2 comments
Closed

Mocking Meteor.user() #111

caio-ribeiro-pereira opened this issue Mar 22, 2014 · 2 comments

Comments

@caio-ribeiro-pereira
Copy link

Hi, I'm new using Laika, and I'd like to know how can I mock Meteor.user() object, testing this function:

Post = new Meteor.Collection('posts');

Post.publish = function(message) {
    var currentUser = Meteor.user();
    this.insert({
          message: message
        , time: new Date()
        , userId: currentUser._id
        , name: currentUser.profile.name
    });
};

I just would like to test if the post was added using a fake Meteor.user() to test it.

This is a simple test which is not working, because of Meteor.user() object:

var assert = require("assert");

suite("Post", function() {  
  test("publish a new post", function(done, server) {
    server.eval(function() {
      Post.find().observe({
       added: function(doc) {
        emit("added", doc);
       }
    });
    Post.publish("Ola!");
    });
    server.once("added", function(doc) {
        assert.equal(doc.message, "Ola!");
      done();
    });
  });
});

Does anybody can help me solving this?

Thanks!

@MarcusRiemer
Copy link

I am currently working around this by simply testing in with a "real" and logged in user. This can be done by calling Accounts.createUser({email: 'a@a.com', password: '123456'}); on the server and something like

Meteor.loginWithPassword('a@a.com', '123456', function() {
  Post = new Meteor.Collection('posts');

  Post.publish = function(message) {
    var currentUser = Meteor.user();
    this.insert({
          message: message
        , time: new Date()
        , userId: currentUser._id
        , name: currentUser.profile.name
    });
  };
}); 

on the client.

@caio-ribeiro-pereira
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants