This package has been deprecated

Author message:

Package renamed to ngx-http-test for future Angular releases

angular2-http-testing
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

angular2-http-testing

Makes testing Http calls as easy as were with AngularJS.

Installation

npm install angular2-http-testing --save-dev

Usage

// First add it as a provider
beforeEach(() => {
  TestBed.configureTestingModule({
    providers: [
      ...
      FakeBackend.getProviders()
    ]
  });
});
 
// Get the instance of it
beforeEach(inject([..., FakeBackend], (..., fakeBackend: FakeBackend) => {
  backend = fakeBackend;
}));
 
// Use it in a test case
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.

// can be object, it will be json stringified if not a string
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', // url
  { username: 'blacksonic', password: 'secret' }, // payload
  { 'Content-Type': 'application/json' } // headers
).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();
});

Readme

Keywords

Package Sidebar

Install

npm i angular2-http-testing

Weekly Downloads

0

Version

1.1.0

License

ISC

Last publish

Collaborators

  • blacksonic