Creating a real unit test in Karma for Angular code [closed]

Posted on

Problem

This is a piece of test code made for the Angular application.

The router object is a mock provided by the RouterTestingModule dependency. I wonder if such a test can be considered a unit test (because it actually tests only one element and the fact that it calls some method – without checking its result), or should it be called an integration test (due to the fact that it still call external dependence)?

it('should trigger the navigation to `/home`', async(() => {
  const link = fixture.debugElement.nativeElement.querySelector('.home-link');

  link.click();

  expect(router.navigateByUrl).toHaveBeenCalled();
}))

Solution

Since

  • router object is a mock, and
  • the fact that it calls some method – without checking its result

You have a unit test, more specifically a whitebox test, as opposed to a blackbox test that tests the output of some method.

For it to become an integration test, you would have use a router instead of a mock.

Leave a Reply

Your email address will not be published. Required fields are marked *