Skip to content

Test Bed Configuration and Injection

TestBed is Angular’s main testing utility.

It creates a testing module similar to Angular modules.

import { TestBed } from "@angular/core/testing";
import { MyService } from "./my.service";
describe("MyService", () => {
let service: MyService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [MyService],
});
service = TestBed.inject(MyService);
});
it("should create service", () => {
expect(service).toBeTruthy();
});
});

TestBed.configureTestingModule() configures the test environment.

TestBed.inject() retrieves service instances.