简体   繁体   English

未调用Laika测试中的回调

[英]Callbacks in Laika tests are not called

Meteor.collection.insert() accepts callback as an argument. Meteor.collection.insert()接受callback作为参数。 As an example, one can create a brand new Meteor project and run the following code in the browser's console. 例如,可以创建一个全新的Meteor项目,并在浏览器的控制台中运行以下代码。

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. 当我使用相同的代码并将其放入Laika测试中时,永远不会调用callback参数。 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? 有人知道为什么在此Laika测试中未调用回调函数吗? This seems to be an issue for more than just Meteor.collection.insert() . 这似乎不仅仅是Meteor.collection.insert()

(I'm running Ubuntu 13.04, Meteor 0.7.0.1, Laika 0.3.1, PhantomJS 1.9.2-6) (我正在运行Ubuntu 13.04,Meteor 0.7.0.1,Laika 0.3.1,PhantomJS 1.9.2-6)

Well, Mr. jonS90, if you were to run Laika with the --verbose flag, you would notice that an exception is quietly being thrown: 好吧,jonS90先生,如果您要使用--verbose标志运行Laika,您会注意到一个异常正在悄然抛出:

[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. 您会看到,在这种情况下您无权访问done() 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();
    })
})

The problem is that you're trying to call done(); 问题是您正在尝试调用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). 实际上,您实际上需要侦听插入到my_collection并发出一个信号,该信号由客户端或服务器(在您的情况下为客户端)接收。 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. 查看https://github.com/arunoda/hello-laika以获得测试的基本示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM