Unit Tests - server-specific services

Last but not least of your unit tests, should be to verify the server-specific services. In this lesson you will learn how to enforce Karma to run tests against code that includes server-specific objects and libraries.

Testing Server-Specific Classes#

You're moving to a more sophisticated part of unit testing in your Angular Universal application. In this lesson, you will create more advanced stubs that you need to perform assertions.

Stubbing MongoDB and TransferState#

Start by installing Sinon.JS that you will use to create stubs:

Create a new file, src/app/products-server.service.spec.ts, then add import statements and set up variables that will hold stubs:

Initialize stubs in the beforeEach() method and provide them in configureTestingModule():

Notice that dbClientStub and transferStateStub are both created using the sinon.stub() method. This lets you check inside tests if the service is calling MongoDB (rather than HTTP) to retrieve products. You will also check if it feeds up TransferState with retrieved products by calling its set() method.

Let's write the tests:

 

This page is a preview of The newline Guide to Angular Universal

No discussions yet