Learning about the subscribe and assert pattern
As we have learned so far, observables are lazy and we don't get 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.
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:
export class SampleService { getValue(value: boolean): Observable<boolean> { return of(value); } }
The test of this method will look like this:
describe('SampleService', () => { let service: SampleService; beforeEach(() => { ...