I'm trying to write Angular (version 12.2.0) unittests the first time and there occured a problem I can't solve.
The following code ist my unittest:
import { ConfirmDialogModule } from 'primeng/confirmdialog';
import { ConfirmationService, SharedModule } from 'primeng/api';
import { TestBed } from '@angular/core/testing';
import { PasswordGenerator } from './password-generator';
describe('PasswordGenerator', () => {
let generator: PasswordGenerator;
let confirmService: ConfirmationService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ConfirmDialogModule, SharedModule],
providers: [ConfirmationService, PasswordGenerator] });
generator = TestBed.inject(PasswordGenerator);
confirmService = TestBed.inject(ConfirmationService);
})
it('instance should be created', () => {
expect(confirmService).toBeTruthy();
});
});
My tested service PasswordGenerator doesn't import any services itself. But my test seems to have a problem with the PrimeNg ConfirmationService of app.module.ts. The ConfirmationService is part of PrimeNg's ConfirmDialogModule. The error message:
NullInjectorError: R3InjectorError(DynamicTestModule)[ConfirmationService -> ConfirmationService]: NullInjectorError: No provider for ConfirmationService!
The service works properly in the running app but I can't figure out how to make him run in the test. Has anyone an idea what I could try so solve the problem or where the origin of the problem is?