3
你好,我每個身體都有一些angular2單元測試的問題。有誰能夠幫助我?
我有一些類這樣
在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 ?
}));
});
我將不勝感激,如果你能幫助我這個東西。 感謝您的關注!
不知怎的,這不是爲我工作和afaik,你只能以這種方式覆蓋injectables(在例子'Http'和'AuthService'中),n ot擴展類:( 有沒有辦法完全交換服務的超類(又名原型)進行測試? – shoesel