2016-07-22 10 views
0

私がやっていることが完全に間違っているかどうかはわかりませんが、私はHTML要素のいくつかを定義するために "指示"から "コンポーネント"に切り替えたとき、私は突然私のカルマテスト。ここで私が持っているものです。基本カルマ角1.5コンポーネントテスト

karam.conf.js

... 
preprocessors: { 
    'module-a/module-a.view.html': ['ng-html2js'], 
    ..., 
    'module-z/module-z.view.html': ['ng-html2js'] 
}, 

ngHtml2JsPreprocessor: { 
    moduleName: 'theTemplates' 
}, 
... 

モジュール-a.component.js

(function(){ 
    "use strict"; 

    angular.module('ModuleA').component('moduleAComponent',{ 
     controller: 'ModuleAController as moduleAVm', 
     templateUrl: 'module-a/module-a.view.html'  
    }); 
})(); 

モジュール-tests.js

は、
"use strict"; 

describe('ModuleA',function(){ 

    beforeEach(module('ModuleA')); 

    describe('Controller',function(){ 
     ... 
    }); 

    describe('Component',function(){ 
     var element, $rootScope; 
     beforeEach(module('theTemplates')); 
     beforeEach(inject([ 
      '$compile','$rootScope', 
      function($c,$rs) { 
       $rootScope = $rs; 
       element = $c('<module-a-component></module-a-component>')($rootScope); 
       $rootScope.$digest(); // ??? 
      } 
     ])); 

     it('should have moduleAVm',function(){ 
      expect(element.html()).not.toBe(''); // FAILS HERE 
      expect(element.html()).toContain('moduleVm'); // FAILS HERE TOO 
     }); 

    }); 

}); 

エラー:

The easiest way to unit-test a component controller is by using the $componentController that is included in ngMock. The advantage of this method is that you do not have to create any DOM elements. The following example shows how to do this for the heroDetail component from above.

そして、それは私に夜が明け、私の(( 'コントローラ'、機能について説明します。

Expected '' not to be ''. 
+0

コンソールに表示されているエラーを追加できますか?また、このangular.module( 'ModuleA'、[])を使ってみてください。component( 'moduleAComponent' .... –

+0

'module-a/module-aの' angular.module( 'ModuleA'、[]) 'を定義しました。しかし、私はエラーを追加しました。期待している呼び出しで失敗しました。これはelement.html()が空であることを意味します。つまり、それは消化していません(私は思います)。 – westandy

答えて

3

OKは、より徹底的に角度のdocumentationを読んだ後、私はこの声明に出くわしました){...});私は実際に変更するのに必要な、と私はちょうど正式として知られ、「コンポーネント」部分を取り除く必要があることを何だった「指令」

ここに今、私の「コントローラー」です:私はまだ、今

beforeEach(inject([ 
     '$componentController', // replaced $controller with $componentController 
     function($ctrl){ 
      ctrl = $ctrl('queryResults',{ // Use component name, instead of controller name 
       SomeFactory:MockSomeFactory, 
       SomeService:MockSomeService 
      }); 
     } 
    ])); 

コンポーネントを同時にテストしながら、私のコントローラをテストしてください。そして、$ compile、$ rootScopeなどを使ってDOM要素を作成する必要がなくなりました。

+0

あなたは '$ componentController'を使うことができる時に要素を作成する必要はありませんか? –

関連する問題