Meteor.collection.insert()接受callback作为参数。例如,可以创建一个全新的Meteor项目,并在浏览器的控制台中运行以下代码。
my_collection = new Meteor.Collection("myCollection");
my_collection.insert(
{some: "object"},
function() {
console.log("finished insertion");
})当我接受相同的代码并将其放入Laika测试中时,callback参数就不会被调用。这是我的测试代码:
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)
发布于 2014-01-11 19:45:14
问题是,当插入回调不存在于该函数作用域中时,您试图在插入回调中调用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();
});
});
})有关测试的基本示例,请查看https://github.com/arunoda/hello-laika。
发布于 2014-01-03 18:50:51
好吧,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();
})
})https://stackoverflow.com/questions/20876850
复制相似问题