2017-07-04 23 views
1

私はcomponent(Angular2アプリ)のunit testKarma-Jasmineで書いています。また、コードカバレッジレポートにはIstanbulを使用しています。spyonを使用した場合のコードカバレッジの問題

は、ここであなたが見ることができるように、私はそれがnextbutton clickで呼び出さかなっているかどうかを確認するためにspying on onNext function午前私のテストケース、

it('Should Invoke onNext function', async(() => { 
    const fixture = TestBed.createComponent(LoginComponent); 
    fixture.detectChanges(); 
    const login = fixture.componentInstance; 

    spyOn(login, 'onNext'); 

    let email = fixture.debugElement.nativeElement.querySelector("input[name='username']"); 
    email.value = "email"; 

    let nextButton = fixture.debugElement.nativeElement.querySelectorAll("button")[1]; 
    nextButton.click(); 

    fixture.whenStable().then(() => { 
     expect(login.onNext).toHaveBeenCalled(); 
    }) 
    })); 

です。それは正常に動作しており、テストに合格します。

しかし、ログインページのコードカバレッジレポートには、onNext関数が含まれていないことが示されています。 enter image description here

何が間違っていますか?

そして、私はonNext機能をスパイしていない場合にも、機能がカバーされ、

it('Should Invoke onNext function', async(() => { 
    const fixture = TestBed.createComponent(LoginComponent); 
    fixture.detectChanges(); 
    const login = fixture.componentInstance; 


    let email = fixture.debugElement.nativeElement.querySelector("input[name='username']"); 
    email.value = "email"; 

    let nextButton = fixture.debugElement.nativeElement.querySelectorAll("button")[1]; 
    nextButton.click(); 
    })); 

enter image description here

+1

を関数呼び出し。 2つのテストを行う必要があります。最初の関数は関数が実際に呼び出されたかどうかをテストし、2番目の関数はonNext関数をテストします。 – trichetriche

+0

そのことを知らなかった。情報をありがとう。それを行う良い方法はありますか?私は同時に両方を達成することができますか? – Vinay

+0

ええ、それを同じ 'it'に入れてください(私はそれをお勧めしませんが)。ところで、 'component.onNext()'を呼び出すことは、コードカバレッジでそれをカバーしますが、テストではありません!テストでは、正しい値を取得するかどうかをテストするためにアサーションを使用する必要があることを意味します。 – trichetriche

答えて

3

この使用:スパイが実際に置き換えるので

spyOn(login, 'onNext').and.callThrough()

+0

まさに私が望むもの..ありがとう – Vinay

関連する問題