Javascript 不调用Laika测试中的回调

Javascript 不调用Laika测试中的回调,javascript,meteor,laika,Javascript,Meteor,Laika,接受回调作为参数。例如,您可以创建一个全新的Meteor项目,并在浏览器控制台中运行以下代码 my_collection = new Meteor.Collection("myCollection"); my_collection.insert( {some: "object"}, function() { console.log("finished insertion"); }) 当我使用相同的代码并将其放入Laika测试中时,回调参数永远不会被调用。

接受
回调
作为参数。例如,您可以创建一个全新的Meteor项目,并在浏览器控制台中运行以下代码

my_collection = new Meteor.Collection("myCollection");
my_collection.insert(
    {some: "object"},
    function() {
        console.log("finished insertion");
    })
当我使用相同的代码并将其放入Laika测试中时,
回调
参数永远不会被调用。以下是我的测试代码:

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();
                })
        })
    })
})
有人知道为什么在这个Laika测试中不调用回调函数吗?这似乎不仅仅是Meteor.collection.insert()的问题


(我正在运行Ubuntu13.04、Meteor 0.7.0.1、Laika 0.3.1、PhantomJS 1.9.2-6)

好吧,jonS90先生,如果您使用
--verbose
标志运行Laika,您会注意到一个异常正在悄然抛出:

[client log] Exception in delivering result of invoking '/myCollection/insert': ReferenceError: Can't find variable: done
您知道,在该上下文中您无权访问
done()
。以下是您应该如何修改代码:

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();
    })
})

问题是您试图调用
done()my_collection
中的插入,并发出一个信号,该信号由客户机或服务器(在您的情况下是客户机)拾取。此外,您显然不会在测试中初始化集合;这应该在生产代码中完成

请尝试以下方法:

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();
            });
    });
})
查看测试的基本示例