Angular Material and Jasmine : " No provider for InjectionToken MdDialogData! "

Wenneguen picture Wenneguen · Jun 29, 2017 · Viewed 49.6k times · Source

I have a component which is meant to be used in an Angular Material MdDialog :

@Component({
  ...
})
export class MyComponent {

  constructor(@Inject(MD_DIALOG_DATA) public data: any, public dialogRef: 
MdDialogRef<MyComponent>) {
...
  }


}

I am trying to Unit Test it with Jasmine :

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        SharedTestingModule,
      ],
      declarations: [
        MyComponent,
      ],
    })
    .compileComponents();
  }));

  ...

});

Unfortunately, I am getting the following error :

Error: No provider for InjectionToken MdDialogData!

SharedTestingModule imports and exports my custom Angular Material module, which itself imports and exports MdDialogModule.

How can I get rid of this error?

Thank you very much!

Angular 4.2.4
Angular Material 2.0.0-beta.7
Jasmine 2.5.3

Answer

Wenneguen picture Wenneguen · Jun 29, 2017

I added this :

providers: [
    { provide: MD_DIALOG_DATA, useValue: {} },
    { provide: MdDialogRef, useValue: {} }
]

And it works :)

Thanks for your help @methgaard!