我正在尝试修复服务调用的代码覆盖率错误。我遵循两种方法,但犯了一些错误。
下面是我正在编写测试用例的方法
setPrefixSuffixDetails(): void {
this.prefixSuffixDetailSubscription = this.profileBeneficiaryService.getPrefixSuffixDetails()
.subscribe(data => {
if (data.body.prefixlist.length > 0) {
this.prefixConfig.options = data.body.prefixlist;
}
if (data.body.suffixlist.length > 0) {
this.suffixConfig.options = data.body.suffixlist;
}
}, error => {
this.loadingData = false;
this.notificationService.addNotications(error);
});
}
Run Code Online (Sandbox Code Playgroud)
为了进行测试,我正在创建提供程序,以下是我的提供程序
{ provide: ProfileBeneficiaryService, useClass: ProfileServiceStub},
{provide: ProfileBeneficiaryService, useClass: ProfileBenficiaryErrorStub},
Run Code Online (Sandbox Code Playgroud)
一种是成功调用,另一种是错误调用。
beforeEach(async(() => {
TestBed.configureTestingModule({ .............
class ProfileBenficiaryErrorStub {
getPrefixSuffixDetails = function () {
return Observable.throw(Error('Test error'));
}}
class ProfileServiceStub {
getPrefixSuffixDetails = function () {
return Observable.of(this.data);
}
}
Run Code Online (Sandbox Code Playgroud)
但问题是,当我使用两个提供程序时,它只会覆盖错误,如果我不包含错误提供程序,它会覆盖成功 …