2017-03-13 61 views
0

我的JS文件:

case "launch": helper.urlLaunch("http://www.google.com").then(function(){start();}); 

urlLaunch的定义

urlLaunch: function (url) { 
      //... 
      return $q.when(); 
     }, 

单元测试

it("should test helper launch url", function() { 
      spyOn(helper, "urlLaunch").and.callFake(function(){}); 
      mySvc.purchase(Url: PURCHASE_URL }); //this calls the "launch" case given above 
      $httpBackend.flush(); 
      expect(helper.urlLaunch).toHaveBeenCalled(); 
     }); 

但是,这给了我一个错误“键入e rror:plan.apply不是函数“

任何想法我在这里错过了什么?

回答

1

你的urlLaunch函数应该返回一个promise,但是你用一个不返回任何东西的假函数来嘲笑它。所以使用返回的承诺的代码实际上会收到undefined。这是行不通的。

您需要返回从暗中监视功能的承诺:

spyOn(helper, "urlLaunch").and.returnValue($q.when('some fake result')); 
mySvc.purchase(Url: PURCHASE_URL }); 
$scope.$apply(); // to actually resolve the fake promise, and trigger the call of the callbacks 

// ... 
+0

奏效!谢谢 –