Kris
Kris

Reputation: 23

Meteor.call() with Jasmine doesn't work properly

I have a problem with Meteor.methods. I need to test quite complicated function but i don't know how get the return value. For my own needs I wrote a trivial code:

Meteor.methods({
  returnTrue: function() {
    return true;
    },
    returnFalse: function(){
    	return false;
    }
   });
Then i wrote also trivial test in Jasmine:

describe("Function", function() {
  var tmp;
  it("expect to be true", function(){
    Meteor.call('returnTrue', function(error, result){
	 if(error){
           tmp = false;
	 }
	 else{
	   tmp = result;
     }
	 });
    expect(tmp).toBe(true);
  });
});

And in my test i have Expected undefined to be true. I was trying to go through it using Session.set and Session.get but with the same result. Any idea how can i do that?

Upvotes: 1

Views: 179

Answers (1)

SylvainB
SylvainB

Reputation: 4820

Here is a question that might peak your interest. A few things are misguided in your code:

  • Your call does not reach the actual server method
  • Your expect is called outside of the method's callback, hence tmp is not set yet!

Here is a suggestion!

describe("Function", function() {
  var tmp;
  it("expect to be true", function(){
    spyOn(Meteor, "call").and.callThrough(); // to actually call the method
    Meteor.call('returnTrue', function(error, result){
     if(error){
           tmp = false;
     }
     else{
       tmp = result;
     }
     expect(tmp).toBe(true); // moved the check inside the callback, once the call is actually executed
     });
    expect(Meteor.call).toHaveBeenCalled(); // check if the method has been called
  });
});

Upvotes: 1

Related Questions