我需要测试一种服务方法,但我不确定如何为调用某些注入服务方法而运行的部分代码实现 100% 的代码覆盖率。
待测服务方式:
@Injectable()
export class BomRevisiosnsService {
constructor(
private baseService: BaseService,
private appConstants: AppConstants,
private dmConstants: DMConstants
) { }
public getRevisionsData(): any {
var itemId = this.appConstants.userPreferences.modelData['basicDetails']['itemId'];
let url = this.dmConstants.URLs.GETBOMREVISIONS + itemId + "/GetRevisionsAsync";
let headers = {
"Content-Type": "application/json",
UserExecutionContext: JSON.stringify(this.appConstants.userPreferences.UserBasicDetails),
}
if (itemId != null || itemId != undefined) {
return this.baseService.getData(url, headers)
.map(response => {
// this area not getting covered.
return response;
});
}
}
}
我如何覆盖:
.map(response => {
return response;
});
在下面的:
测试:
it('should return response if itemId is not null or undefined', () => {
let mockData = [1,2,3];
let mockObservable = Observable.of(mockData);
spyOn(baseService, 'getData').and.returnValue(mockObservable);
appConstants.userPreferences.modelData['basicDetails']['itemId']=4; // itemId not null or undefined
dMConstants.URLs.GETBOMREVISIONS ="dummy url";
subject.getRevisionsData();
expect(baseService.getData).toBe(mockData); // How do I test .map()?
});