-
-
Save adammendoza/bc064f002a9c4d9813f49b48205b3fc3 to your computer and use it in GitHub Desktop.
Angular HttpClient (2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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