Writing simple isolated unit test

Unit tests are a very important part of the programming process. Over the years it gains more and more traction. Going the path from exotics and, sometimes, weird implementation, extreme practices like TDD. From the geek’s toy to the mainstream. From the back-end to the front-end. Now unit tests are a “must-have” tool in the professional programmer’s toolset. This small article will tell about the front-end unit testing with Angular by using built-in test tools Karma and jasmine.

Types of automated tests.

There are 3 main types of automated tests. There are no strict borders between them and it can be arguably hard to decide which test falls in which category.

Unit testing — test of one “unit” of code. It can be a class, or method, with mocked dependencies. We will use this type of test here.

Integration testing — Few classes testing integrated. For example, for the back-end, it can be service and repository testing together, or service and component testing for the front-end.

End to end testing — full application testing, from front-end to back-end, that’s why it is called a (front) end to (back) end testing. In these tests usually live services using and nothing is mocked. it fully reproduces the real application work.

The task — unit test of the method.

Let’s imagine that we already have the simple Angular application — well known “Tour of heroes”. And we want to validate the name of the hero by its length. Let’s imagine that we don’t want the hero’s name less than 3 symbols.

#angular #unit-testing #unit-test-tools

Unit testing in Angular
1.75 GEEK