【发布时间】:2018-01-04 14:28:48
【问题描述】:
大家好,我在 angular2 单元测试中遇到了一些问题。有谁能够帮我?
我有这样的课程
export class PostcodeApiService extends ApiService {
constructor(Http: Http, auth: AuthService) {
super(Http, auth);
}
getPostcodes(filterObject) {
return super.post('postcodes/filter', filterObject );
}
}
export class ApiService {
constructor(private http: Http) {
}
post(url, payload: any) {
return new Observable((observer) => {
this.http.post(`someUrl/${url}`, payload)
.map((res: Response) => res.json())
.catch((error: any) => {
return Observable.throw(error || `Server error ${url}`);
})
.subscribe((data) => {
observer.next(data);
});
});
}
}
如何模拟 ApiService.post() 函数?
我的规范文件是这样的
describe('PostcodeApiService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
PostcodeApiService,
Http,
ConnectionBackend,
AuthService,
UserService
],
imports: [
HttpModule
]
});
});
it('should ...', inject([PostcodeApiService], (service: PostcodeApiService) => {
// How ovveride post method ?
}));
});
如果您能帮我解决这个问题,我将不胜感激。 感谢关注!
【问题讨论】:
标签: angular unit-testing typescript