I'm trying to understand how to use the mocking functionality provided as part of the new HttpClientModule. My test code matches what's shown in the existing documentation almost exactly. However, the doc example is not complete - for example, at a minimum the import statement for TestBad and inject is missing. I assume the same is true of HttpClient from @angular/common/http. My test code adds these.
This test fails, as req ends up being undefined after the httpMock.expectOne call. I'd appreciate any insight into why that might be.
import { TestBed, inject } from '@angular/core/testing';
import { HttpClient } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
describe('HttpClientTestingModule', () => {
beforeEach(() => TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ],
providers: [ HttpClient, HttpTestingController]
}));
it('expects a GET request', inject([HttpClient, HttpTestingController], (http: HttpClient, httpMock: HttpTestingController) => {
http
.get('/data')
.subscribe(data => expect(data['name']).toEqual('Test Data'));
const req = httpMock.expectOne('/data');
expect(req).toBeDefined();
expect(req.request.method).toEqual('GET');
req.flush({name: 'Test Data'});
httpMock.verify();
}));
});