我试图测试一个进行http调用的方法,并且也在我的类的构造函数中调用
我的课程是:
export class Service {
constructor(http: Http) {
this.loadData();
}
loadData(): void {
// Make http call
}
}
我的测试是:
let service;
let mockBackend;
beforeEach(inject([BopUserService, MockBackend], (_service, _backend) => {
service = _service;
mockBackend = _backend;
}));
it('should load data', () => {
mockBackend.connections.subscribe(connection => {
connection.mockRespond(new Response(new ResponseOptions({body: response})));
});
service.loadData();
// Assert data has been loaded
});
我收到一条错误,说明http调用中的observable未定义。如何模拟构造函数中的http调用和我的测试中的http调用的响应?