Lesson 15

Creating a Mock Backend

WARNING: This module is deprecated and no longer receives updates. Protractor is likely being removed as the default from Angular applications and Protractor itself will likely stop receiving updates and development in the future. I would recommend checking out the Test Driven Development with Cypress/Jest as a replacement.

Faking responses from a server to isolate unit tests

DEPRECATED

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, useClass: ModulesMock },
  { provide: NavController, useClass: NavMock },
];

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:

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).