In your test, you use Http, not HttpClientModule, anyway, to fake the response from httpclient in the service test, since I set it up for the test.
import {TestBed, getTestBed} from '@angular/core/testing';
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [AuthService]
});
authService = TestBed.get(AuthService);
httpMock = TestBed.get(HttpTestingController);
});
it('should return user and token successfully at login', (done) => {
const credentials = {userName: 'bob', password: '123'} as LoginPayload;
const httpResponse = {user: user, token: '123'} as LoginSuccessPayload;
authService.login(credentials).subscribe(res => {
expect(res).toEqual(httpResponse);
done();
});
const loginRequest = httpMock.expectOne('/api/auth/signin');
loginRequest.flush(httpResponse);
expect(loginRequest).toBeDefined();
expect(loginRequest.request.method).toEqual('POST');
httpMock.verify();
});
source
share