在Meteor中使用Jasmine测试异步函数 [英] Testing an async function with Jasmine in Meteor

查看:82
本文介绍了在Meteor中使用Jasmine测试异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Stackoverflow上看了几个与此相关的其他问题,但我似乎无法解决我的问题。无论我做什么,似乎Meteor.call都没有被调用,或者我可以调用它(例如在下面的代码示例中),无论 jasmine.DEFAULT_TIMEOUT_INTERVAL 设置为,我继续收到以下错误:

I have looked at several other questions related to this on Stackoverflow, but I still can't seem to solve my problem. No matter what I seem to do, it seems that either Meteor.call doesn't get invoked, or if I can get it to be invoked (such as in the code sample below), no matter what the jasmine.DEFAULT_TIMEOUT_INTERVAL is set to, I continue to get the following error:

错误:超时 - 异步回调是在jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时时间内没有调用。

这是我的Jasmine测试的样子:

This is what my Jasmine test looks like:

it("Should be created and not assigned to anyone", function(done) {
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000000;

    // Confirm that the User Has Logged in
    expect(Meteor.userId()).not.toBeNull();

    var contact = null;
    var text = "This is a testing task";
    spyOn(Tasks, "insert");
    spyOn(Meteor, "call");

    Meteor.call('addTask', contact, text, function(error, result) {
      expect(error).toBeUndefined();
      expect(result).not.toBeNull();
      done();
    });

    expect(Meteor.call).toHaveBeenCalled();

  });

});

我的addTask函数如下所示:

And my addTask function looks like this:

Meteor.methods({

  addTask: function (contact, text) {
     ... // addTask Code, removed for brevity
  },
});

我已经坚持了几个星期,任何人都可以提供任何帮助都会非常有帮助。

Iv been stuck on this for weeks, any help anyone can provide would be super helpful.

推荐答案

由于Jasmine没有调用原始的.call()方法,因此从未执行过hander中的期望。为了使它工作,而不是 spyOn(Meteor,call); 你应该写 spyOn(Meteor,call)。and.callThrough (); 在执行Jasmine间谍逻辑后将调用原始处理程序。

The expectations inside the hander are never executed, because Jasmine does not invoke the original .call() method. To make it work, instead of spyOn(Meteor, "call"); you should write spyOn(Meteor, "call").and.callThrough(); which will call the original handler after executing Jasmine spying logic.

这篇关于在Meteor中使用Jasmine测试异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆