2016-05-30 18 views
1

コントローラをユニットテストしたいです。私はAPIを期待する基本的なテストアサーションから始めました。しかし、私は条件チェックの中でスコープメソッドを嘲笑することに挑戦しています。スコープの下で利用できないのでundefinedエラーが発生しました。グローバルlogout()メソッドしか利用できません。カルマエラー - 定義されていないと予想されます。

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

_someMethod_を_if_の外に定義して、それを動作させるために必要なパラメータを渡すのはなぜですか? –

+0

@min che:もし私が間違っていたら私を修正してください..私の意図は、認証されたユーザーのためだけにそのメソッドを実行することです..私はテストのためにそれを外しています...それを無視していませんか?また、なぜそれが動作していないと、それを回避するために行くの代わりに働くことを理解しようとしている.. – RVR

答えて

1

私はそれが仕事を得ることができました。 問題は、コントローラの起動時にlocalStorageServiceにisAuthenticatedがtrueに設定されていないことでした。コントローラーを呼び出す前に、に設定して置きます。

+1

ありがとう..多くのこと..私はdidntこれらの行..でも考えて:-) – RVR

+0

それその問題を解決しました。しかし、コントローラを起動する前に** beforeEach()**ブロックに配置しているので、新しいものを開きます。コントローラー内のすべてのhttpサービス呼び出しをトリガーしています。私はスコープメソッドが定義されているかどうかを宣言したいだけです。 – RVR

+0

私はhttp呼び出しが行われた場所を示すためにコントローラを変更しました。これは...特定の機能をアサートしようとしているすべてのユニットテストケースが、各テストケースでhttpコールを解決/ – RVR

関連する問題