ERROR Nullinjectorerror: R3injectorerror(Dynamictestmodule)[Kdsdialogservice -> Matdialog -> Matdialog]: Nullinjectorerror: No Provider for Matdialog

Asked

Viewed 23 times

0

I am doing unit testing in an application of mine. I am beginner regarding testing, so I need your help.

In my application I do a service using Matdialog ( Kdsdialogservice).

I have tried putting many import alternatives, my service or matdialog as providers I have no more idea what to do

export declare class KDSDialogService {
    dialog: MatDialog;
    private dialogRef;
    constructor(dialog: MatDialog);
    open(componentOrTemplateRef: ComponentType<any> | TemplateRef<any>, title?: string, data?: any, size?: DialogSize, showClose?: boolean): MatDialogRef<any, any>;
    static ɵfac: ɵngcc0.ɵɵFactoryDef<KDSDialogService, never>;
}

And on my home.component.spec I import and make the statements here, but still I get this error. Again, I’ve tried putting as Provider the service but I haven’t managed yet, here are alternatives of my despair

describe('HomeComponent', () => {
   let component: HomeComponent;
   let fixture: ComponentFixture<HomeComponent>;

   beforeEach(async(() => {
      TestBed.configureTestingModule({
         declarations: [HomeComponent ],
         imports:[KDSDialogService, MatDialogModule],
  
      }).compileComponents();
   }));

   beforeEach(() => {
      fixture = TestBed.createComponent(HomeComponent);
      component = fixture.componentInstance;
    
   });

   it('should create', () => {
      fixture.detectChanges();
      expect(component).toBeTruthy();
   });
});

Error print

1 answer

0

He is complaining about a Preview you use in the Kdsdialogservice class and when creating the component in the test you are passing Matdialogmodule as import when it should be an item of the providers attribute. Try the following where you create the component with Testbed:

   beforeEach(async(() => {
      TestBed.configureTestingModule({
         declarations: [HomeComponent ],
         imports:[KDSDialogService],
         providers: [MatDialogModule}
  
      }).compileComponents();
   }));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.