0

I want to test with jasmine a snackbar service. More specific, I'm testing the following two cases:

  1. That the service is created
  2. The method inside it to be called

snackbar.service

import { Injectable, NgZone } from '@angular/core';
import { MatSnackBar } from '@angular/material';

@Injectable({
  providedIn: 'root'
})
export class SnackbarService {

  constructor(
    public snackBar: MatSnackBar,
    private zone: NgZone
  ) { }

  public open(message, action, duration = 1000) {
    this.zone.run(() => {
      this.snackBar.open(message, action, { duration });
    })
  }
}

snackbar.service.spec

import { TestBed } from '@angular/core/testing';
import { SnackbarService } from './snackbar.service';

describe('SnackbarService', () => {
  beforeEach(() => TestBed.configureTestingModule({}));

  it('should be created', () => {
    const service: SnackbarService = TestBed.get(SnackbarService);
    expect(service).toBeTruthy();
  });

  it('should call open()', () => {
    const service: SnackbarService = TestBed.get(SnackbarService);
    const spy = spyOn(service, 'open');
    service.open('Hello', 'X', 1000);
    expect(spy).toHaveBeenCalled();
  })
});

After running the tests, Karma gives me the following errors:

  1. SnackbarService > should call open() NullInjectorError: StaticInjectorError(DynamicTestModule)[MatSnackBar]: StaticInjectorError(Platform: core)[MatSnackBar]: NullInjectorError: No provider for MatSnackBar!
  2. SnackbarService > should be created NullInjectorError: StaticInjectorError(DynamicTestModule)[MatSnackBar]: StaticInjectorError(Platform: core)[MatSnackBar]: NullInjectorError: No provider for MatSnackBar!

Any ideas on how should I fix this issue?

Thanks!

blk
  • 27
  • 1
  • 6

1 Answers1

1

Yes you have to import and provide what is needed.

import { TestBed } from '@angular/core/testing';
import { SnackbarService } from './snackbar.service';
import { MatSnackBarModule } from '@angular/material/snack-bar';

describe('SnackbarService', () => {
  let zone: NgZone;
  let snackBar: MatSnackBar;
  beforeEach(() => TestBed.configureTestingModule({
     imports: [MatSnackBarModule],
     providers: [
       SnackbarService,
       NgZone,
     ],
  }));

  beforeEach(() => {
    // if you're on Angular 9, .get should be .inject
    zone = TestBed.get(NgZone);
    spyOn(zone, 'run').and.callFake((fn: Function) => fn());
    snackBar = TestBed.get(MatSnackBar);
  });

  it('should be created', () => {
    const service: SnackbarService = TestBed.get(SnackbarService);
    expect(service).toBeTruthy();
  });

  // the way you have written this test, it asserts nothing
  it('should call open()', () => {
    const service: SnackbarService = TestBed.get(SnackbarService);
    // const spy = spyOn(service, 'open');
    const spy = spyOn(snackBar, 'open');
    service.open('Hello', 'X', 1000);
    expect(spy).toHaveBeenCalled();
  })
});

I have never unit tested something requiring NgZone but look into this if you get into issues (Running jasmine tests for a component with NgZone dependency).

AliF50
  • 16,947
  • 1
  • 21
  • 37
  • 1
    Thanks for your help! I did as you said and ran the tests again. This error popped: Error: Can't resolve all parameters for NgZone: (?). Then I tried fixing it with the link that you shared. Unfortunately, no luck. – blk Mar 19 '20 at 14:12
  • 1
    Managed to fix it! I removed the ngZone from my service and no need to worry about it anymore. – blk Mar 19 '20 at 14:24