Subscribe and Assert Pattern
Learn about the subscribe and assert pattern in detail.
We'll cover the following
Introduction to testing RxJS observables
Many developers consider testing observables a challenging task. This is true. But if we learn the right techniques, we can implement maintainable and readable tests in a very effective manner.
In this chapter, we’ll walk through three commonly used patterns for testing streams. We’ll start by explaining the subscribe and assert pattern, after which we’ll discuss the marble-testing pattern. Finally, we’ll highlight a suitable pattern for testing streams that are returned from HTTPClient by focusing on a concrete example in our recipe app.
Implementing the Subscribe
and Assert
pattern
As we’ve learned, observables are lazy, and we don't get any value until we subscribe to them. In tests it’s the same thing—observables won’t emit any value until we subscribe to them. To solve this, programmers always tend to subscribe to the observables manually inside the tests.
Let’s suppose we have to test a method called getValue(value: boolean)
that’s available in an Angular service called SampleService
. This method is very simple: it returns an observable that will emit the Boolean
value that’s given as input, as follows:
Get hands-on with 1400+ tech skills courses.