Lesson 15

Creating a Mock Backend

Faking responses from a server to isolate unit tests

PRO

Lesson Outline

Creating a Mock Backend

We keep hitting on the point that a unit test should be isolated - nothing comes in, and nothing goes out. If the unit test requires some input from an external dependency then we supply it with fake data.

In order to keep with this principle, we have been substituting our injected dependencies with mocked versions, like this:

providers: [
  { provide: ModulesService, useValue: {/* etc */} },
  { provide: NavController, useValue: {/* etc */} },
];

In tests that we are about to create, we will be dealing with code that makes an HTTP request to a server. We don't want the code in that test to actually send a request to our real server, we just want to check that the individual unit is doing its job - not that the entire system is working (again, that's not the role of a unit test).

When we want to make HTTP requests, we inject the HttpClient service into our constructor. Given the approach we have been using for mocking services so far, you would be correct in assuming that you should do something like this:

      providers: [
        ...
        { provide: HttpClient, useValue: {/* etc */} },
        ...
      ]
PRO

Thanks for checking out the preview of this lesson!

You do not have the appropriate membership to view the full lesson. If you would like full access to this module you can view membership options (or log in if you are already have an appropriate membership).