arunoda / laika

testing framework for meteor
http://arunoda.github.io/laika/
MIT License
242 stars 38 forks source link

Mocking Meteor.user() #111

Closed caio-ribeiro-pereira closed 10 years ago

caio-ribeiro-pereira commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

Thanks!