TestBed configures an isolated Angular testing environment and provides access to services or components under test. For signal-based services, it supplies the setup needed to create a stub component and trigger change detection manually. section 8.3.5
Listing 8.15 Using a stub component to test a service with signals
@Component({
selector: 'app-stub',
template: '',
standalone: true,
})
export class StubComponent { #1
constructor(private readonly service: TimeOffManagementService) {}
}
describe ('TimeOffManagementService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [MockProvider(TimeOffRequestService, MockTimeOffRequestService)],
imports: [StubComponent], #2
});
localStorageMock = {
getItem: jest.fn().mockReturnValue(() => selectedType),
setItem: jest.fn().mockImplementation((key, value) => {
selectedType = value;
})
};
Object.defineProperty(window, 'localStorage', {
value: localStorageMock,
});
service = TestBed.inject(
TimeOffManagementService,
); #3
});
it('should be successfully instantiated', () => {
expect(service).toBeTruthy();
});
it('should have all requests loaded initially, () => {
const fixture = TestBed.createComponent(StubComponent);
fixture.detectChanges(); #4
expect(service.requests())
.toEqual(mockRequests); #5
});
});
Use the stub-component listing to see TestBed configuration, dependency mocking, service injection, and the test environment setup together. section 8.3.5

