Skip to content

Latest commit

 

History

History
48 lines (33 loc) · 936 Bytes

11-http-testing.md

File metadata and controls

48 lines (33 loc) · 936 Bytes

1. Setup

git checkout ut-14-component-output-testing

yarn

2. Run tests

yarn test farmicode --watch

3. Query ReST API

Animal search ReST API is at http://localhost:3000 and it takes a q parameter for querying animal names: http://localhost:3000/?q=dolly.

  1. Import HttpClientTestingModule:
TestBed.configureTestingModule({
  imports: [HttpClientTestingModule],
});
  1. Inject HttpTestingController.

  2. Trigger search with AnimalSearch.search.

  3. Use HttpTestingController to grab the request and flush an arbitrary response:

const request = httpController.expectOne('/');
request.flush([
  {
    type: 'dog',
    name: 'Rocket',
    bornAt: '2020-01-01',
  },
]);

You can match the request using a predicate httpController.expectOne(req => true).

  1. Check the response.

  2. Check that the request contains the q parameter with the right value.