Skip to content

Instantly share code, notes, and snippets.

@metric152
Created December 21, 2017 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metric152/4783d7a6482dd02872bf39af952b91b3 to your computer and use it in GitHub Desktop.
Save metric152/4783d7a6482dd02872bf39af952b91b3 to your computer and use it in GitHub Desktop.
Testing an Angular 5 HTTP call
import { TestBed, async, inject } from '@angular/core/testing';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ HttpClientModule, HttpClientTestingModule ]
});
}));
// Clear the http controller
afterEach(inject([HttpTestingController], (backend: HttpTestingController) => {
backend.verify();
}));
it('should mock an http call', async(inject([HttpClient, HttpTestingController], (http, backend) => {
http.get('/test').subscribe(
data => {
expect(data).toBeTruthy();
},
error => {}
);
backend.match({
url: '/test',
method: 'GET'
})[0].flush({'fu': 'bar'});
})));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment