Angular Unit Testing

Quick Look – Angular Unit Testing

by 

| November 13, 2018 | in

Unit testing is a valuable part of any development process. It isn’t as useful as many developers make it out to be; there are many things developers should be focused on more than unit tests. Code review is one such thing.

But just because it isn’t the most important thing, doesn’t mean we shouldn’t be doing it. Unit tests are a useful piece in a layered approach to quality.

Angular makes it super easy to run unit tests. Just run “ng test”.

Starting with our previous Angular example, we will write a unit test for our contact service. Unit testing our contact service isn’t super easy, because our contact service depends upon HttpClient. That means we will have to mock out the HttpClient.

Mocking with Angular is super easy.

First off you need to describe what we want to test.

We need to set up our imports for the tests.

We need to inject these modules into each test.

Call the actual method we want to test.

Provide a mock result.

Unit testing your client-side code, yes you can. We can get too focused on unit testing, it is just one part of a layered approach to quality. But having the ability to do it for your client code too is really nice tool to have in the toolbox.

Unit Test Full Source

Contacts Service


Related posts