4

I have tried to get these tests to run many times. I'm just really not sure and can't seem to find why it is wrong. I am using the storage module on my Ionic app and when trying to add it to the unit test I get the below error. I'm pretty lost so any help would be really appriciated.

NullInjectorError: StaticInjectorError(DynamicTestModule)[AuthService -> Storage]: StaticInjectorError(Platform: core)[AuthService -> Storage]: NullInjectorError: No provider for Storage!

import { TestBed, fakeAsync, tick, inject } from '@angular/core/testing';
import {
  HttpClientTestingModule,
  HttpTestingController,
} from '@angular/common/http/testing';
import { AuthService } from './auth.service';
import { HttpErrorResponse } from '@angular/common/http';
import { IonicStorageModule } from '@ionic/Storage';

import { of } from 'rxjs';

describe('AuthServiceService', () => {
  let service: AuthService;
  let httpTestingController: HttpTestingController;
  beforeEach(() => {

    TestBed.configureTestingModule({
      providers: [
        AuthService
      ],
      imports: [
        HttpClientTestingModule,
        IonicStorageModule.forRoot()
      ],
    });
    httpTestingController = TestBed.get(HttpTestingController);
    service = TestBed.get(AuthService);

  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('should run HandleError 1 time', fakeAsync(() => {
    spyOn(service, 'handleError').and.callThrough();
    service.handleError(new HttpErrorResponse({error: 'error'}));
    tick();
    expect(service.handleError).toHaveBeenCalledTimes(1);
  }));

  it('should call login 1 time', inject([IonicStorageModule], (storage: IonicStorageModule) => {
    const serviceSpy = spyOn(service as AuthService, 'login');
    service.login('');
    expect(serviceSpy.calls.count()).toBe(1);
  }));

  it('should call logout 1 time', () => {
    const serviceSpy = spyOn(service as AuthService, 'logout');
    service.logout('');
    expect(serviceSpy.calls.count()).toBe(1);
  });

  it('should call getAuthenticationDetails 1 time', () => {
    const serviceSpy = spyOn(service as AuthService, 'getAuthenticationDetails').and.callThrough();
    service.getAuthenticationDetails();
    expect(serviceSpy).toHaveBeenCalledTimes(1);
  });

  it('should call setIsAuthenticated 1 time', () => {
    const serviceSpy = spyOn(service as AuthService, 'setIsAuthenticated').and.callThrough();
    service.setIsAuthenticated(true, 'myaccount');
    expect(serviceSpy).toHaveBeenCalledTimes(1);
  });

  it('should call setIsAuthenticated 1 time when type is ros', () => {
    const serviceSpy = spyOn(service as AuthService, 'setIsAuthenticated').and.callThrough();
    service.setIsAuthenticated(null, 'ros');
    expect(serviceSpy).toHaveBeenCalledTimes(1);
  });

  it('should call login 1 time', () => {
    const serviceSpy = spyOn(service as AuthService, 'login').and.callThrough();
    service.login('');
    expect(serviceSpy).toHaveBeenCalledTimes(1);
  });

  it('should call logout 1 time', () => {
    const serviceSpy = spyOn(service as AuthService, 'logout').and.callThrough();
    service.logout('');
    expect(serviceSpy).toHaveBeenCalledTimes(1);
  });

  it('throws 404 error', () => {
    service.login('').subscribe(
      data => fail('Should have failed with 404 error'),
      (error: HttpErrorResponse) => {
        expect(error.status).toEqual(404);
        expect(error.error).toContain('404 error');
      }
    );
  });

  it('throws 401 error', () => {
    service.login('').subscribe(
      data => fail('Should have failed with 401 error'),
      (error: HttpErrorResponse) => {
        expect(error.status).toEqual(401);
        expect(error.message).toContain('401 error');
      }
    );
  });

});
SmiffyKmc
  • 581
  • 1
  • 10
  • 28
  • Maybe this is helpful: https://stackoverflow.com/questions/55813831/ionic-4-creating-mock-storage – brandt.codes May 28 '20 at 09:00
  • 1
    My friend, if I could buy you a beer or any beverage of life I sure would. I have a good bit to go in learning Karma/Jasmine, it still throws me. But thank you. If you'd like to make it an answer, I will accept it. – SmiffyKmc May 28 '20 at 09:18
  • 1
    You are welcome. Good luck and have fun. We all still learning ;-) – brandt.codes May 28 '20 at 09:25
  • Just out of interest, if I have a page which uses the Auth service which uses the Storage, should I inject the storage into my page testing? – SmiffyKmc May 28 '20 at 09:25
  • Yes :-/ But maybe https://github.com/ike18t/ng-mocks is something for you? – brandt.codes May 28 '20 at 09:28
  • 1
    Okay seems I still have a way to go. I have a SignIn which uses Auth. But it's giving out about the Auth in SignIn not having the Storage provider. The Auth service test works now though which the question was based on so thank you :) – SmiffyKmc May 28 '20 at 09:37

0 Answers0