2016-05-30 20 views
1

我想單元測試我的控制器。我從預期API的基本測試斷言開始。但是我在條件檢查中嘲笑範圍方法面臨挑戰。我收到undefined錯誤,因爲它在範圍內不可用,只有全局的logout()方法可用。Karma錯誤 - 預計未定義要定義

我試圖嘲笑localStorageService使用spyOn爲真,以滿足條件,但這仍然沒有幫助。任何解決方案都會幫助我啓動。

控制器:

angular.module('app').controller('sampleCtrl', 

     function($scope, $state, $http, $rootScope, localStorageService) { 

      if (!(localStorageService.get('isAuthenticated'))) { 

       $state.go('home'); 

      } 
      if (localStorageService.get('isAuthenticated') === true) { 

       //http post calls made here to perform certain operation on page load 

       $scope.someMethod = function(){ 

        //do something 

        } 

      } 

      $scope.logOut = function() { 

       localStorageService.set('property', ''); 

       localStorageService.set('isAuthenticated', false); 

       $state.go('home'); 

      }; 
}); 

噶:

'use strict'; 

describe('Controller: sampleCtrl', function() { 

    /** to load the controller's module */ 
    beforeEach(module('app')); 

    var sampleCtrl,scope,httpBackend,deferred,rootScope; 

    beforeEach(inject(function ($controller,_$rootScope_,$httpBackend,$q) { 

     var store = {}; 
     scope= _$rootScope_.$new(); // creates a new child scope of $rootScope for each test case 
     rootScope   = _$rootScope_; 
     localStorageService = _localStorageService_; 
     httpBackend   = $httpBackend; 

     httpBackend.whenGET(/\.html$/).respond(''); 

     spyOn(localStorageService, 'set').and.callFake(function (key,val) { 
      store[key]=val; 
     }); 

     spyOn(localStorageService, 'get').and.callFake(function(key) { 
      return store[key]; 
     }); 

     sampleCtrl = $controller('sampleCtrl',{ 
      _$rootScope_:rootScope, 
      $scope:scope, 
      $httpBackend:httpBackend, 
      _localStorageService_:localStorageService 
      // add mocks here 
     }); 

     localStorageService.set('isAuthenticated',true); 

    })); 

    /**ensures $httpBackend doesn’t have any outstanding expectations or requests after each test*/ 
    afterEach(function() { 
     httpBackend.verifyNoOutstandingExpectation(); 
     httpBackend.verifyNoOutstandingRequest();  
    }); 


    it('sampleCtrl to be defined:',function(){ 

     httpBackend.flush(); 
     expect(sampleCtrl).toBeDefined(); 

    }); 

    // failing test case - scope.someMethod not available in scope 
    it('is to ensure only authenticated user can access the state methods',function(){ 
      localStorageService.get('isAuthenticated'); 
      httpBackend.flush(); 
      expect(scope.someMethod).toBeDefined(); 
    }); 


}); 
+0

你爲什麼不在_if_之外定義_someMethod_,只傳遞它所需的參數? –

+0

@min che:糾正我,如果我錯了..我的意圖是執行該方法只爲認證的用戶..如果我爲了測試而將其移出條件..不是它違抗它?另外...即時通訊試圖瞭解爲什麼它不工作,以及如何使其工作..而不是去尋求解決方法.. – RVR

回答

1

我已經成功地得到它的工作。 問題是localStorageService在啓動控制器時沒有isAuthenticated設置爲true。在調用控制器之前將其設置爲true

+1

謝謝much..i沒有想到在這些行.. :-) – RVR

+0

它解決了這個問題..但它打開了一個新的..因爲我把它放在** beforeEach()**塊之前啓動控制器..它觸發控制器中的所有http服務調用,即使對於其中的測試用例我只是想斷言一個範圍方法是否定義。 – RVR

+0

我修改了控制器以顯示http調用的地方..由於這...所有的單元測試用例,我試圖斷言一個特定的功能正在受到影響迫使我在每個測試用例中解析/模擬http調用 – RVR