2016-04-07 8 views
0

私は以下のコントローラを持っています。ユニットテストの間、私は最初にすべてのコントローラのプロパティと関数が個々のロジックをユニットテストする前に定義されていることをテストします。AngularJS-コントローラの機能がジャスミンを使って定義されていることをテストする方法

angular.module('sampleModule') 
    .controller('SampleController', SampleController); 

    SampleController.$inject =['sampleService']; 

    function SampleController(sampleService){ 
     this.property1 = 'some data'; 
     this.property2 = 'some other data'; 

     this.getData = function(){ 
     //do something 
     } 
     this.postAttributes = function() {  
     sampleService.updateMethod(number,attributes) 
      .then(function(response){ 
       //do something on successful update 
      },function(response){ 
       //do something on unsuccessful update 
      }); 
     }; 
    } 

ここに私が使用しているサンプル仕様があります。 $ controllerサービスを使用してSampleControllerインスタンスを作成した後、コントローラのプロパティが定義されていることを確認できます。定義する未定義の期待 :私は機能上の同じアサーションを実行するとき はしかし、私は、関数が

describe('SampleController Test', function(){ 
    var $controller; 
    var service; 

    beforeEach(angular.mock.module('sampleModule')); 

    beforeEach(angular.mock.inject(function(_$controller_){ 
     $controller = _$controller_; 
    })); 

    it('Testing $scope variable', function(){ 
     var sampleController = $controller('SampleController', { 
      sampleService: service, //mocked factory service 
     }); 

     expect(sampleController.property1).toBeDefined(); 
     expect(sampleController.property2).toBeDefined(); 
     expect(sampleController.getData).toBeDefined(); //this assetion fails 
    }); 

}); 

第三assetionは以下のエラーで失敗し、未定義であるというエラーを取得します。

何が欠けていますか?そして、それはすべてのコントローラのプロパティをテストする正しいアプローチですか、そして、関数は個々のロジックをテストする前に定義されていますか?

答えて

0

この変更を行ってください。私たちは** SampleControllerにスコープの新しいインスタンスを注入する必要がある理由

describe("\n\nuser registration form testing", function() { 


    describe("SampleController Test", function() { 

     var scope; 

     beforeEach(angular.mock.module('sampleModule')); 
     beforeEach(inject(function ($rootScope,_$controller_) { 
      $controller = _$controller_; 
      scope = $rootScope.$new(); 
     })) 

     it('Testing $scope variable', function(){ 

      var sampleController = $controller('SampleController',{$scope: scope}); 
      expect(sampleController.property1).toBeDefined(); 
      expect(sampleController.property2).toBeDefined(); 
      expect(sampleController.getData).toBeDefined(); //this assetion fails 
     }); 

    }); 


}); 
+0

あなたはプロパティのいずれもスコープdirectly..what役割にバインドされていない場合スコープはここに遊ぶん**説明していただけますか? – Pramodh

+0

私の間違い申し訳ありません。 –

+0

スコープを挿入せずに関数が定義されているかどうかをテストする方法はありませんか? – Pramodh

関連する問題