简体   繁体   中英

Testing an async function with Jasmine in Meteor

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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