MatDialog Service Unit Test Angular 6 Error : <p>I'm having modal service to open, confirm and close dialog and i am making its unit test file but i got and error on Angular and this is the code.</p>

<p><strong>modal.service.ts</strong></p>

<pre><code>@Injectable()
export class ModalService {

  constructor(private dialog: MatDialog) { }

  public open&lt;modalType&gt;(modalComponent: ComponentType&lt;modalType&gt;): Observable&lt;any&gt; {
    let dialogRef: MatDialogRef&lt;any&gt;;

    dialogRef = this.dialog.open(modalComponent, {
      maxWidth: '100vw'
    });
    console.log(dialogRef)
    dialogRef.componentInstance.body = body;

    return dialogRef.afterClosed().pipe(map(result =&gt; console.log('test'); );
  }

}
</code></pre>

<p>modal.service.spec.ts</p>

<pre><code>export class TestComponent  {}


describe('ModalService', () =&gt; {
  let modalService: ModalService;

  const mockDialogRef = {
    open: jasmine.createSpy('open')
  };

  beforeEach(async(() =&gt; {
    TestBed.configureTestingModule({
      imports: [ MatDialogModule ],
      providers: [
        ModalService,
        MatDialogRef,
        { provide: MatDialog, useClass: MatDialogStub }
      ]
    }).compileComponents();

    modalService = TestBed.get(ModalService);
  }));


  it('open modal', () =&gt; {
    modalService.open(DummyComponent, '300px');
    expect(modalService.open).toHaveBeenCalled();

  });

});
</code></pre>

<p>So with that code  the error is</p>

<pre><code>TypeError: Cannot read property 'componentInstance' of undefined
</code></pre>

<p>Can you help me how to make this successful? Help is much appreciated.</p>
