Meteor.call() 与 Jasmine 无法正常工作

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

我对 Meteor.methods 有疑问。我需要测试相当复杂的功能,但我不知道如何获得 return 值。为了我自己的需要,我写了一个简单的代码:

Meteor.methods({
  returnTrue: function() {
    return true;
    },
    returnFalse: function(){
     return false;
    }
   });
然后我在 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);
  });
});

在我的测试中,我期望未定义为真。 我试图使用 Session.set 和 Session.get 来完成它,但结果相同。 知道我该怎么做吗?

是一个可能会引起您兴趣的问题。您的代码中有几件事被误导了:

  • 您的调用没有到达实际的服务器方法
  • 您的 expect 是在方法回调之外调用的,因此 tmp 尚未设置!

这是一个建议!

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