私はcomponent
(Angular2アプリ)のunit test
をKarma-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関数が含まれていないことが示されています。
何が間違っていますか?
そして、私は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();
}));
を関数呼び出し。 2つのテストを行う必要があります。最初の関数は関数が実際に呼び出されたかどうかをテストし、2番目の関数はonNext関数をテストします。 – trichetriche
そのことを知らなかった。情報をありがとう。それを行う良い方法はありますか?私は同時に両方を達成することができますか? – Vinay
ええ、それを同じ 'it'に入れてください(私はそれをお勧めしませんが)。ところで、 'component.onNext()'を呼び出すことは、コードカバレッジでそれをカバーしますが、テストではありません!テストでは、正しい値を取得するかどうかをテストするためにアサーションを使用する必要があることを意味します。 – trichetriche