jonS90
jonS90

Reputation: 1379

Callbacks in Laika tests are not called

Meteor.collection.insert() accepts callback as an argument. As an example, one can create a brand new Meteor project and run the following code in the browser's console.

my_collection = new Meteor.Collection("myCollection");
my_collection.insert(
    {some: "object"},
    function() {
        console.log("finished insertion");
    })

When I take this same code and put it in a Laika test, the callback argument never gets called. Here is my test code:

suite('testing Laika out', function() {
    test('inserting into collection', function(done, server, client) {
        client.eval(function() {
            my_collection = new Meteor.Collection("myCollection");
            my_collection.insert(
                {some: "object"},
                function() {
                    console.log("finished insertion");
                    done();
                })
        })
    })
})

Anyone know why the callback function isn't called in this Laika test? This seems to be an issue for more than just Meteor.collection.insert().

(I'm running Ubuntu 13.04, Meteor 0.7.0.1, Laika 0.3.1, PhantomJS 1.9.2-6)

Upvotes: 0

Views: 201

Answers (2)

jonS90
jonS90

Reputation: 1379

Well, Mr. jonS90, if you were to run Laika with the --verbose flag, you would notice that an exception is quietly being thrown:

[client log] Exception in delivering result of invoking '/myCollection/insert': ReferenceError: Can't find variable: done

You see, you don't have access to done() in that context. Here's how you should revise your code:

test('inserting into collection', function(done, server, client) {
    client.eval(function() {
        my_collection = new Meteor.Collection("myCollection");

        finishedInsertion = function () {
            console.log("finished insertion");
            emit('done')
        }
        my_collection.insert(
            {some: "object"},
            finishedInsertion)
    })
    client.once('done', function() {
        done();
    })
})

Upvotes: 0

Jim Kirkbride
Jim Kirkbride

Reputation: 389

The problem is that you're trying to call done(); inside of your insert callback, when it doesn't exist in that function scope. You actually need to listen for the insertion into my_collection and emit a signal which is picked up by either the client or server (the client in your case). Also, you obviously won't be initializing your collection in your test; that should be done in your production code.

Try this instead:

var assert = require("assert");

suite('testing Laika out', function() {
    test('inserting into collection', function(done, server, client) {

        client.eval(function() {
            addedNew = function(newItem) {
                console.log("finished insertion");
                emit("done", newItem)
            };
            my_collection = new Meteor.Collection("myCollection");
            my_collection.find().observe({
                added: addedNew
            });
            my_collection.insert(
                {some: "object"}
            )
        }).once("done", function(item) {
                assert.equal(item.some, "object");
                done();
            });
    });
})

Check out https://github.com/arunoda/hello-laika for the basic examples for testing.

Upvotes: 0

Related Questions