我有一个 Angular 服务发出一些 HTTP 请求,我想测试它是否使它们以正确的方式。
在它的依赖项中,它还有一个EnvironmentService
根据环境返回正确的 API 端点,我用 Jasmine 模拟过。
我的测试如下所示:
describe('ShipmentsService', () => {
let httpTestingController: HttpTestingController;
let service: ShipmentsService;
const envSpy = jasmine.createSpyObj('EnvironmentService', ['getRestEndpoint', 'dev']);
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
ShipmentsService,
{provide: EnvironmentService, useValue: envSpy}
],
imports: [HttpClientTestingModule]
});
httpTestingController = TestBed.get(HttpTestingController);
service = TestBed.get(ShipmentsService);
});
it('does the right HTTP request', () => {
envSpy.getRestEndpoint.and.returnValue('foo');
service.doStuff(123);
expect(envSpy.getRestEndpoint)
.toHaveBeenCalledWith('changePackingProperties');
const request = httpTestingController.expectOne('foo');
request.flush({shipmentId: 123});
httpTestingController.verify();
});
});
这是我的ShipmentsService
方法:
doStuff(shipmentId: number) {
let path = this.environmentService.getRestEndpoint('changePackingProperties');
return this.http.post(path, body, {headers}).pipe(map(
(response) => response
));
}
我错过了什么?