Skip to content

Instantly share code, notes, and snippets.

@dherges
Last active June 1, 2018 07:09
Show Gist options
  • Save dherges/3d2a376d5eb5f1129783b5be2e4ca876 to your computer and use it in GitHub Desktop.
Save dherges/3d2a376d5eb5f1129783b5be2e4ca876 to your computer and use it in GitHub Desktop.
Angular HttpClient (2)
import { TestBed, async, inject } from '@angular/core/testing';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
describe(`FakeHttpClientResponses`, () => {
beforeEach(() => {
// 0. set up the test environment
TestBed.configureTestingModule({
imports: [
// no more boilerplate code w/ custom providers needed :-)
HttpClientModule,
HttpClientTestingModule
]
});
});
it(`should issue a request`,
// 1. declare as async test since the HttpClient works with Observables
async(
// 2. inject HttpClient and HttpTestingController into the test
inject([HttpClient, HttpTestingController], (http: HttpClient, backend: HttpTestingController) => {
// 3. send a simple request
http.get('/foo/bar').subscribe();
// 4. HttpTestingController supersedes `MockBackend` from the "old" Http package
// here two, it's significantly less boilerplate code needed to verify an expected request
backend.expectOne({
url: '/foo/bar',
method: 'GET'
});
})
)
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment