Testing an async function with Jasmine in Meteor

2019-07-13 03:04发布

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:

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

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

  });

});

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.

1条回答
家丑人穷心不美
2楼-- · 2019-07-13 03:47

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.

查看更多
登录 后发表回答