作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要测试其中一种服务方法,但我不确定如何为调用某些注入(inject)的服务方法而运行的部分代码实现 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()?
});
最佳答案
it('should return response if itemId is not null or undefined', () => {
...
const spy = spyOn(baseService.getData).and.return(mockedData)// you need to define data returned by baseService.getData
const result = subject.getRevisionsData();
expect(spy).toHaveBeenCalled(); // How do I test .map()?
expect(result).toEqual(mockedData);// for simple map it will be the same thing that is returned for more complicated map it may differ
});
关于javascript - Angular 单元测试 : How to unit test . map()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56667636/
我是一名优秀的程序员,十分优秀!