0

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?

Lisa Em
  • 1
  • 2
  • could you please provide code of your confirmation service? – Andrei Oct 25 '21 at 09:59
  • @Andrei that isn't my confirmation service. It's the confirmation service of PrimeNg that is contained in the ConfirmDialogModule. I could only provide the .d.ts file of PrimeNg but I don't know if I'm allowed to as it isn't my code. – Lisa Em Oct 25 '21 at 10:17
  • hm, could you try to put service=... stuff in a separate beforeEach? – Andrei Oct 25 '21 at 10:27
  • Thank you! But I figured out that the problem were the other spec files. It seems like if one isn't working then none of the other spec files runs properly. – Lisa Em Oct 25 '21 at 10:49

0 Answers0