无法在伪造的异步测试中进行XHR [英] Cannot make XHRs from within a fake async test
问题描述
Karma/Jasmine的新手寻求帮助.我正在尝试运行以下测试,但出现错误无法在伪造的异步测试中进行XHR".我已经包含了我要调用的测试和方法.任何帮助,我们将不胜感激.
Total novice at Karma/Jasmine looking for some assistance. I'm trying to run the following test and I get the error "Cannot make XHRs within a fake async test". I have included the test and the method I am attempting to call. Any help is greatly appreciated.
import...
fdescribe('CageService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
HttpModule
],
providers: [
BaseRequestOptions,
MockBackend,
CageService,
{ provide: 'appHttpService', useClass: AppHttpService },
{ provide: 'appHttpHelperService', useClass: AppHttpHelperService },
{ provide: 'appUtilityService', useClass: AppUtilityService },
{ provide: Http, useFactory: (backend: ConnectionBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
}, deps: [MockBackend, BaseRequestOptions] }
]
});
});
it('will load cages', inject([CageService, MockBackend], fakeAsync(( cageService, mockBackend\) => {
var res;
mockBackend.connections.subscribe( c => {
expect(c.request.url).toBe('http://example.com');
let response = new ResponseOptions( { body: '{"name": "charles"}' } );
c.mockRespond( new Response( response ) );
});
cageService.load({}).subscribe((_res) => {
res = _res;
});
tick(100);
discardPeriodicTasks();
expect(res.name).toBe('Charles');
})));
});
我正在调用的方法读取
load ( criteria: Object ) {
return this.appHttpService.get( this.url + '?' + criteria )
.map(
response => {
let data = response.json();
this.pagination.total = data.count;
this.pagination.per_page = data.limit;
this.pagination.current_page = data.currentPage;
this.pagination.last_page = data.totalPages;
let cages = [];
for( let x = 0; x < data.rows.length; x++ ) {
cages.push( this.formatCage(new Cage(), data.rows[x] ) );
}
this._cages$.next( this.dataStore.cages );
return data.rows;
}
);
}
推荐答案
Angular 6+解决方案.
首先对于6+角,我们必须使用拦截器来处理. 您需要创建一个实现HttpIntercepter的服务,并且只需重写"intercept"方法,它应以所需的任何值返回Observer.
First of all for angular 6+ we have to use Interceptors to deal with that. You need to create a service that implements HttpIntercepter and just override 'intercept' method, it should return Observer with any value you want.
我遇到了同样的错误,我的解决方法是
I faced with same error, and my solution is
@Injectable()
class TestHttpRequestInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler):
Observable<HttpEvent<any>> {
return new Observable<any>(observer => {
observer.next({} as HttpEvent<any>);
});
}
}
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [SharedModule, RouterTestingModule,
StoreModule.forRoot(fromReducers.reducer))
],
declarations: [],
providers: [
LocalStorageService,
{
provide: HTTP_INTERCEPTORS, useClass: TestHttpRequestInterceptor, multi: true
}
],
schemas: [NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA]
})
.compileComponents();
}));
希望代码会有所帮助.
这篇关于无法在伪造的异步测试中进行XHR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!