2016-12-30 21 views
3

コンポーネントを検証するこのメソッドがあり、それはViewChildという別のコンポーネントを呼び出しています。私はテストしようとしているコンポーネントのthis.ViewChildComponent.someMethod();を使ってコンポーネントを使用しています。私はspyOn(viewChildComponent, 'someMethod').and.returnValue(true)を使ってみました。 しかしそれはthis.ViewChildComponent.someMethod() is undefinedと言います。私はプロバイダー上のViewChildComponentのサービスのようなすべての依存関係を持っています。私は一歩前進して、viewChildComponentが決定するためにそのサービスにするという電話をした。someMethod();Jasmineを使用してviewChildから別のメソッドを呼び出すメソッドをテストします。

何か助けが素晴らしいだろう。

+0

があるために働いべき簡潔な私のしたいがために、依存関係を満たすために方法を知っていますViewChild – HarmonicaBlower

答えて

2

あなたがテストspecファイルdeclarateの子コンポーネントの例

class TestedComponent() { 
    @ViewChild('ChildComp') public variableChildComponent: ChildComp; 
} 

とテストコンポーネントを持っている場合:

beforeEach(() => { 
    TestBed.configureTestingModule({ 
    declarations: [ TestedComponent, ChildComp] 
    }) 
    fixture = TestBed.createComponent(TestedComponent); 
    comp = fixture.componentInstance; 
    spyOn(TestedComponent.variableChildComponent, 'someMethod').and.returnValue(true); 
}); 

関連する問題