Нет поставщика для хранения в тесте кармы - PullRequest
1 голос
/ 28 мая 2020

Я пытался запустить эти тесты много раз. Я просто не уверен и не могу понять, почему это неправильно. Я использую модуль хранения в своем приложении Ioni c, и при попытке добавить его в модульный тест я получаю следующую ошибку. Я очень потерян, так что любая помощь будет действительно полезна. Нет провайдера для хранилища!

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');
      }
    );
  });

});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...