The anatomy of a unit test
There are many different ways to test a piece of code. In this chapter, we will look at the anatomy of a unit test—the separate parts it’s made of.
To test any code, we need a framework for writing the test and a runner to run it on. In this section, we will focus on the test framework. The test framework should provide utility functions for building test suites containing one or several test specs. As a result, unit testing involves the following concepts:
- Test suite: A suite that creates a logical grouping for many tests. A suite, for example, can contain all the tests for a specific feature.
- Test spec: The actual unit test.
We will use Jasmine in this chapter, a popular test framework that is also used by default in Angular CLI projects. This is what a unit test looks like in Jasmine:
describe('Calculator', () => {
it('should add two numbers', () => {
expect(1+1).toBe(2)...