Testing Effects: Testing the Success Case
Explore how to effectively unit test the success scenario of NgRx effects in Angular applications. Understand using the testScheduler, marble testing, and mocking service responses to validate asynchronous effect logic and dispatched actions.
Introduction
The ProductsEffects have been defined so that, when the loadProductsAction is dispatched from our application, the loadProducts$ effect detects it by subscribing to the actions$ observable in lines 17–18. Then, in order to fetch the product data, our effects connect to the server in line 19. If the product data is successfully returned from the server, we have dispatched another action called getProductsAction() in line 20.
Let’s unit test this success case in this lesson.
Let’s visualize the success case in the following illustration:
Unit testing the success case
We can unit test the success case by following the steps below.
Defining the specs
Let’s use the it function to define the specs of our test in lines 32–34.
Executing the testScheduler
In the ProductsEffects, we have interacted asynchronously with the server via the ProductsService to fetch the data. To test this asynchronous behavior, let’s use the testScheduler in lines 34–36.
The testScheduler has a run function, which receives a callback function as its parameter. This callback function itself receives a RunHelper ...