2017-07-14 18 views
0

私はdocumentation for testing Angular 1.5 componentsに従っています。

$ componentControllerが未定義のエラーが表示されます。私はそれが何かシンプルだと確信していますが、私はここで何が間違っているかを見ることはできません

ここは私のテストコンポーネントです。ここで

class MyComponent { 
    constructor() { 
    this.counter = 0; 
    } 
    $onChanges() {} 
    $onDestroy() {} 
    addToCounter() { 
    this.counter += 1; 
    } 
} 

MyComponent.$inject = []; 

angular.module('my.component', []) 
    .component('MyComponent', { 
     controller: MyComponent, 
     bindings: {}, 
     template: ` 
     <div>Hello World</div> 
     `, 
}); 

はジャスミンはまず

LOG: '$componentController', undefined 
... 
TypeError: undefined is not a constructor (evaluating '$componentController('MyComponent', null, bindings)') in ... 
+0

テスト用に正しい角度と角度のモックがインストールされていますか? – Chris

+0

はい両方の角度1.5.9 –

+0

コードに問題がなければ、設定をもう一度確認します。 –

答えて

0

を出力している、あなたは "MyComponentの" "myComponentという" に、コンポーネントからコンポーネント名を変更するために必要なもののテストここで

describe('module: my.component', function() { 

    var $componentController; 

    beforeEach(module('my.component')); 

    beforeEach(inject(function(_$componentController_) { 
     $componentController = _$componentController_; 
    })); 

    it('should default counter to 0', function() { 
     var bindings = {}; 
     console.log('$componentController', $componentController); 
     var ctrl = $componentController('MyComponent', null, bindings); 
     expect(ctrl.counter) 
      .toEqual(0); 
    }); 
}); 

です名前は小文字で始まる必要があります。
は、私は少し注入でコントローラをフェッチすることにより、テストを変更:

describe("Just some test",() => { 
    it("should fetch controller",() => { 
    expect(controller).toBeDefined(); 
    }); 
    it("counter should be 0",() => { 
    expect(controller.counter).toBe(0); 
    }); 
    it("should increase counter value to 1",() => { 
    controller.addToCounter(); 
    expect(controller.counter).toBe(1); 
    }); 
}); 

そして、彼らはすべて合格:

let controller; 
beforeEach(() => { 
    angular.mock.module('my.component'); 
    inject(($componentController) => { 
    controller = $componentController("myComponent"); 
    }); 
}); 

そうした後、私はこれらの3つのテストを実行しました。

+0

私がしなければならなかったのは、 "MyComponent"を "myComponent"に変更したことでした。 –

0

my.componenetのMyComponent isteadを試してください

関連する問題