2017-03-13 57 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

奏效!謝謝 –