2013-12-16 58 views
0

我的控制器中有以下代碼,我想爲此部分編寫Jasmine測試用例。 我試着寫一個,但下面的錯誤及其投擲 類型錯誤:[對象對象數組]有沒有方法「然後」無法在ANgularJS中爲Controller編寫Jasmine測試用例

控制器代碼::

$scope.doGetList = function() { 
       var queryString = {......sending some query parameters}; 
       searchResource.getList(queryString).then(
        function (data) { 
         $scope.sugesstions = data; 
        } 
       ); 
      }; 

茉莉花測試用例::

it("should return provided list", angular.mock.inject(function($rootScope, $controller) { 
          var scope = $rootScope.$new(); 

          var searchResource = { 
           getList: function() { 
            return ['suggestions1', 'suggestions2', 'suggestions3']; 
           } 
          }; 

          $controller(
            headerController, 
            { 
             $scope: scope, 
             cartsService: null, 
             currentUser: null, 
             searchResource: searchResource 
            } 
          ); 

          expect(scope.sugesstions).toBeDefined(); 
          expect(scope.sugesstions.length).toBe(0); 

          //this method will call mock method instead of actual server call 
          scope.doGetAutocomplete(); 
          expect(scope.sugesstions.length).toBe(3); 
          expect(scope.sugesstions[0]).toEqual('suggestions1'); 
          expect(scope.sugesstions[1]).toEqual('suggestions2'); 
          expect(scope.sugesstions[2]).toEqual('suggestions3'); 
         })); 

我該怎麼寫。

+0

我仍然不能夠破解了這一點...任何建議? – IfOnly

回答

1

你必須在run()中包裝異步調用。從茉莉DOC:http://pivotal.github.io/jasmine/#section-Asynchronous_Support

或者,我用茉莉花作爲-承諾將與更好的支持:https://github.com/ThomasBurleson/jasmine-as-promised

+0

我不確定如何使用它。如果有任何示例代碼,那麼這將有所幫助 – IfOnly

+0

是不是在這兩個鏈接的例子? – Tino

+0

但是這些例子是不同的,對這種情況不是很有幫助 – IfOnly

相關問題