Learning about the subscribe and assert pattern
As you will already know, Observables are lazy, and we don’t obtain any value until we subscribe to them. In tests, it is the same thing; Observables will not emit any value until we subscribe to them. To solve this, programmers always tend to subscribe to the Observables manually inside the tests and then perform assertions on the emitted values. This is what we call the subscribe and assert pattern.
Let’s delve into testing using the subscribe and assert pattern across three distinct scenarios. We will demonstrate testing for methods returning a single value, methods returning multiple values, and methods returning timed values (values returned after a specified time duration).
Testing single-value output methods
Let’s suppose we have to test a method that returns a single value. The method is called getValue(value: boolean)
and is available in an Angular service called SampleService
.
The method itself...