Created
December 21, 2017 21:36
-
-
Save metric152/4783d7a6482dd02872bf39af952b91b3 to your computer and use it in GitHub Desktop.
Testing an Angular 5 HTTP call
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('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