git checkout ut-14-component-output-testing
yarn
yarn test farmicode --watch
Animal search ReST API is at http://localhost:3000
and it takes a q
parameter for querying animal names: http://localhost:3000/?q=dolly
.
- Import
HttpClientTestingModule
:
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
});
-
Inject
HttpTestingController
. -
Trigger search with
AnimalSearch.search
. -
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)
.
-
Check the response.
-
Check that the request contains the
q
parameter with the right value.