AI Hints
Home›Angular Tutorial›Testing a Service, Testing User Actions and Practice: Test the Basket
Angular · Lesson 106 of 120

Testing a Service

Check the logic.

⏱ 2 min read▶ Video at 2:07:46🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Services hold the important logic, like the basket total. They're also the easiest things to test, because there's no page involved.

  • Services hold the logic
  • Easy to test
  • No page needed

TestBed.inject

Test bed dot inject gives a test its own instance of a service. Before each runs before every test, so each test starts fresh. Then we call methods, and check the signals, with expect.

keyvalue
TestBed.inject(Basket)get the service
beforeEach(...)a fresh one per test
basket.add(...)call methods
expect(basket.total())check the result

basket.ts

The basket keeps a private list. Count, and total, are computed from it. Add puts an item in. And clear empties it.

basket.tstypescript
import { Service, signal, computed } from '@angular/core';

export interface Item { title: string; price: number; }

@Service()
export class Basket {
  private readonly list = signal<Item[]>([]);
  readonly count = computed(() => this.list().length);
  readonly total = computed(() => this.list().reduce((sum, b) => sum + b.price, 0));

  add(item: Item) { this.list.update(l => [...l, item]); }
  clear() { this.list.set([]); }
}

Testing the basket

Before each test, we get a fresh basket. The first test checks that it starts empty. The second adds two books, then checks the count, and the total. To be close to allows for tiny rounding errors, with prices. Run it. Both tests passed.

basket.spec.tstypescript
import { TestBed } from '@angular/core/testing';
import { Basket } from './basket';

describe('Basket', () => {
  let basket: Basket;
  beforeEach(() => basket = TestBed.inject(Basket));

  it('starts empty', () => {
    expect(basket.count()).toBe(0);
  });

  it('adds books and totals the prices', () => {
    basket.add({ title: 'Dune', price: 9.99 });
    basket.add({ title: 'Emma', price: 6.99 });
    expect(basket.count()).toBe(2);
    expect(basket.total()).toBeCloseTo(16.98);
  });
});
Output
 ✓ basket.spec.ts > Basket > starts empty
 ✓ basket.spec.ts > Basket > adds books and totals the prices
 Test Files  1 passed (1)
      Tests  2 passed (2)
Also uses basket.ts
import { Service, signal, computed } from '@angular/core';

export interface Item { title: string; price: number; }

@Service()
export class Basket {
  private readonly list = signal<Item[]>([]);
  readonly count = computed(() => this.list().length);
  readonly total = computed(() => this.list().reduce((sum, b) => sum + b.price, 0));

  add(item: Item) { this.list.update(l => [...l, item]); }
  clear() { this.list.set([]); }
}
Also uses _mode
test

Summary

Test bed dot inject gives each test a fresh service, so you can call its methods, and check the results. Next, testing user actions.

Key takeaways
  • TestBed.inject()
  • beforeEach()
  • toBeCloseTo()
Next up: Testing User Actions — clicks in tests