Topic: How to implement Modal / ModalRef in Angular Unit Tests priority asked 3 months ago

*Expected behavior*Being able to do unit tests with components that use the MdbModal / MdbModalRef

*Actual behavior*Receiving an error:NullInjectorError: R3InjectorError(DynamicTestModule)[MdbModalRef -> MdbModalRef]: NullInjectorError: No provider for MdbModalRef!

I have tried importing different Modules, like MdbModalModule or setting MdbModalRef in providers, but nothing has worked.

What is the standard way to implement the MdbModal in Unit Tests?

Resources (screenshots, code snippets etc.)

Test File:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core';
import { MdbModalModule, MdbModalRef } from 'mdb-angular-ui-kit/modal';

import { AcceptChangesDialogComponent } from './accept-changes-dialog.component';

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

  beforeEach(async(() => {
  imports: [MdbModalModule, MdbModalRef, TranslateModule.forRoot()],
  declarations: [AcceptChangesDialogComponent],

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

  it('should create', () => {


import { Component } from '@angular/core';
    import { MdbModalRef } from 'mdb-angular-ui-kit/modal';

  selector: 'accept-changes-dialog',
  templateUrl: './accept-changes-dialog.component.html'
export class AcceptChangesDialogComponent {
  public header: string | undefined;
  public icon = 'save';
  public content: string | undefined;
  public action = false;

      constructor(public modalRef: MdbModalRef<AcceptChangesDialogComponent>) {

  public cancel(): void {
    this.action = false;

  public accept(): void {
    this.action = true;

Rafał Seifert staff answered 3 months ago

I could not recreate your exact error. We are preparing mock component and module inside spec file like so:

  template: ` <div class="modal-header"></div> `,
  providers: [MdbModalService],
class BasicModalComponent {
  constructor(public modal: MdbModalService) {}

  declarations: [BasicModalComponent],
  imports: [BrowserModule],
class TestModalModule {}

And then we import this mocked module:

      imports: [MdbModalModule, TestModalModule],

Could you please try to implement such approach and let me know if that works for you?

Shandu priority commented 1 day ago

Your example doesn't have ModalRef, where the actual problem lies

Please insert min. 20 characters.


Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.



Specification of the issue

  • ForumUser: Priority
  • Premium support: Yes
  • Technology: MDB Angular
  • MDB Version: MDB5 5.2.0
  • Device: Desktop
  • Browser: Chrome
  • OS: Windows
  • Provided sample code: No
  • Provided link: No