angular2-http-testing
Makes testing Http calls as easy as were with AngularJS.
Installation
npm install angular2-http-testing --save-dev
Usage
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
...
FakeBackend.getProviders()
]
});
});
beforeEach(inject([..., FakeBackend], (..., fakeBackend: FakeBackend) => {
backend = fakeBackend;
}));
it('should call fake endpoint', (done) => {
backend.expectGET('users/blacksonic').respond({ username: 'blacksonic' });
subject.getProfile('blacksonic').subscribe((response) => {
expect(response).toEqual({ username: 'blacksonic' });
done();
});
})
It is possible to specify every detail of the response.
let responseBody = { username: 'blacksonic' };
let responseStatus = 200;
let responseHeaders = { 'Content-Type': 'application/json' };
backend
.expectGET('users/blacksonic')
.respond(responseBody, responseStatus, responseHeaders);
For requests outside of GET the request body can be also specified.
backend.expectPost(
'usernamepassword/login',
{ username: 'blacksonic', password: 'secret' },
{ 'Content-Type': 'application/json' }
).respond(responseForm);
Convenience methods available for different kind of requests:
expectGet
, expectPost
, expectDelete
, expectPut
, expectPatch
, expectHead
, expectOptions
.
After the tests run it is possible to check for outstanding connections or expectations that are not addressed.
afterEach(() => {
backend.verifyNoPendingRequests();
backend.verifyNoPendingExpectations();
});