arunoda / laika

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

Running Client before Server #116

Closed caio-ribeiro-pereira closed 10 years ago

caio-ribeiro-pereira commented 10 years ago

Hi again!

Is there a way to simulate a client execution first and server assert after?

For exemple, only clients can publish a post, so it'll be something like...

var assert = require("assert");

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

I have some functions which only client can execute (because it needs a logged user), and I don't know how to simulate this kind of test.

I will wait some answer, Thanks!

arunoda commented 10 years ago

You can do it with our evalSync API. see:

var assert = require("assert");

suite("Post", function() {  
  test("publish a new post", function(done, server, client) {
    client.evalSync(function() {
       Post.insert({message: "hi!"}, function() {
        emit('return');
       });
    });

    var docs = server.evalSync(function() {
      var docs = Post.find().fetch();
      emit('return', docs);
    });

    assert.equal(docs[0].message, "hi!");
    done();
  });
});

And for the login, you can create a user on every test. Like below.

client.evalSync(function() {
  Accounts.createUser({username: "arunoda", password, "abc"}, function() {
    emit('return');
  });
});

may be you can create a common function for this, where you can use in any testCase you want.

caio-ribeiro-pereira commented 10 years ago

This is my new book about Meteor https://casadocodigo.refersion.com/l/d88.3525 (Brazilian portuguese) today is on beta version, but in the full version it will have a chapter about testing using laika and thats why I requested a help here :)

Thanks @arunoda !!

arunoda commented 10 years ago

Awesome. Yep, I'll help.