我有一个服务从两个不同的后端(通过http请求的数据库和socket.io上的实时数据)获取数据,然后将这些数据作为单个流发布。在测试这项服务时,我希望能够控制模拟http服务的延迟,以便我可以通过不同类型的比赛条件进行游戏。目前我正在这样做:
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
DataService,
{
provide: IoService,
useClass: MockIoService
},
{
provide: Http,
useFactory: (mockBackend, options) => {
return new Http(mockBackend, options);
},
deps: [MockBackend, BaseRequestOptions]
},
MockBackend,
BaseRequestOptions
]
});
});
it('should get data from http backend', async(inject([DataService, MockBackend, IoService], (service: DataService, mockBackend, ioService) => {
const mockResponse = {
data: [
{ id: 0, text: 'httpData', start: 0, stop: 1 }
]
};
mockBackend.connections.subscribe((connection) => {
connection.mockRespond(new Response(new ResponseOptions({
body: JSON.stringify(mockResponse)
})));
});
service.subscribe('test').subscribe((data) => {
expect(data.text).toBe('httpData');
});
})));
这又有效,但我希望能够为MockBackend
定义一个延迟,以便在一定的秒数后响应。这是可能的,如果可以,怎么样?