NashTech Insights

Mocking Dependencies and Using Spies Effectively in Angular

Alka Vats
Alka Vats
Table of Contents


Unit testing is an integral part of software development, and Angular provides powerful tools and techniques to facilitate the testing process. One common challenge in unit testing is dealing with dependencies, such as services and external APIs.

Mocking dependencies and using spies effectively can greatly enhance the quality and reliability of your tests. In this blog post, we will explore how to mock dependencies and employ spies in Angular, accompanied by practical examples.

If you want to learn about the testing features of angular, you can refer here.

Mocking Dependencies:

When writing unit tests, it’s crucial to isolate the unit under test from its dependencies to focus solely on its behavior. Mocking dependencies involves creating dummy objects that mimic the behavior of the real dependencies but without relying on their actual implementation.

Angular’s testing framework provides various mechanisms to achieve this:

a. TestBed: The TestBed is a powerful testing utility in Angular that allows you to configure. It creates instances of components, services, and other dependencies. By using TestBed, you can substitute real dependencies with mock versions.

b. Jasmine spies: Jasmine, the testing framework used in Angular, provides spies that enable you to track function calls, return values, and other interactions. Spies can be used to replace methods of the original dependencies, allowing you to control their behavior during testing.

Using Spies Effectively:

Spies offer valuable capabilities for verifying that functions are called with the correct arguments, observing their return values, and tracking the number of invocations.

Here are a few scenarios where spies can be employed effectively:

a. Function Calls: You can spy on a method to ensure that it is called with the expected arguments. For example, if you have a service method that makes an HTTP request, you can spy on the HTTP client’s post method to verify that it is called with the correct URL and payload.

b. Return Values: Spies can also be used to control the return values of functions. By using the and.returnValue() method, you can force a method to return a specific value, enabling you to simulate different scenarios in your tests.

c. Observing Invocations: Spies allow you to track the number of times a function is called. Thus, this is useful for ensuring that a method is invoked the expected number of times or to detect unexpected multiple invocations.

Example: So, Let’s consider a simple Angular component that depends on a service for data retrieval and manipulation. We want to test the component’s behavior without actually relying on the service implementation.

// my-component.component.ts
import { Component } from '@angular/core';
import { DataService } from './data.service';

  selector: 'app-my-component',
  template: `{{ data }}`,
export class MyComponent {
  data: string;

  constructor(private dataService: DataService) {}

  loadData(): void { = this.dataService.getData();

To mock the DataService dependency and use spies effectively, we can create a test case using the TestBed and Jasmine spies:

// my-component.component.spec.ts
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MyComponent } from './my-component.component';
import { DataService } from './data.service';

describe('MyComponent', () => {
  let component: MyComponent;
  let fixture: ComponentFixture<MyComponent>;
  let dataService: DataService;

  beforeEach(() => {
      declarations: [MyComponent],
      providers: [DataService], // Provide the real DataService

    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    dataService = TestBed.inject(DataService); // Inject the real DataService
    spyOn(dataService, 'getData').and.returnValue('Mocked data'); // Mock the getData method

  it('should load data correctly', () => {
    expect('Mocked data');

Therefore, In this example, we use TestBed to configure and create an instance of MyComponent. We provide the real DataService but create a spy on its getData method using spyOn. By setting the return value of the spy to 'Mocked data', we simulate the behavior of the real service without making actual HTTP requests. Finally, we verify that the component correctly loads and displays the mocked data.


Mocking dependencies and using spies effectively are essential techniques for unit testing in Angular. By isolating the unit under test and controlling the behavior of its dependencies, can write more reliable and focused tests. Hence, Angular’s testing framework, combined with Jasmine spies, provides powerful tools for achieving this. Employing these techniques will enhance your ability to catch bugs early, and improve code quality. It increases overall confidence in your application’s behavior.

Finally, for more such posts, please follow our LinkedIn page- FrontEnd Competency.

Alka Vats

Alka Vats

Alka Vats is a Software Consultant at Nashtech. She is passionate about web development. She is recognized as a good team player, a dedicated and responsible professional, and a technology enthusiast. She is a quick learner & curious to learn new technologies. Her hobbies include reading books, watching movies, and traveling.

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: