How To Mock an Angular Service with Dependency Inversion

A valuable technique for writing a mock service for our test harness and for running our application in a full mock mode

Itchimonji
CP Massive Programming

--

How To Mock an Angular Service with Dependency Inversion

Protecting our Angular code with tests has many advantages:

  • Starting a test suite is much faster and more efficient than running the Angular application. Using smoke tests every time we change our code is not a very efficient way to check if something is broken. “Test-Driven Development by Example” by Kent Beck is a great book describing all these advantages of using a test harness.
  • Code refactoring (also a good book by Marting Fowler) is much easier with a test harness because we need to run our unit tests to check whether something works or is broken while we refactor our code. Otherwise, we need to start our application (ng serve), wait for it to compile and load, and click through it to discover if something is broken (ahh, these boring smoke tests). This costs a lot of time!
  • Every developer could find easy examples inside the test harness, how to use the code and how it works. Sometimes, this is a better understandable way than combining through a complex business logic (maybe our application), especially for a developer who has never seen the code before.

--

--

Itchimonji
CP Massive Programming

Freelancer | Site Reliability Engineer (DevOps) / Kubernetes (CKAD) | Full Stack Software Engineer | https://patrick-eichler.com/links