我试图使这些测试运行多次。我真的不确定,似乎也找不到原因。我在Ionic应用程序上使用存储模块,当尝试将其添加到单元测试中时,出现以下错误。我很迷茫,所以任何帮助都会得到真正的帮助。
NullInjectorError:StaticInjectorError(DynamicTestModule)[AuthService->存储]: StaticInjectorError(平台:核心)[AuthService->存储]: NullInjectorError:没有用于存储的提供程序!
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');
}
);
});
});