Smi*_*Kmc 5 jasmine karma-jasmine angular ionic4
我曾多次尝试让这些测试运行。我只是真的不确定,似乎无法找到为什么它是错误的。我在 Ionic 应用程序上使用存储模块,当尝试将其添加到单元测试时,出现以下错误。我很迷失所以任何帮助都会非常感激。
NullInjectorError:StaticInjectorError(DynamicTestModule)[AuthService -> Storage]:StaticInjectorError(平台:核心)[AuthService -> Storage]: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');
}
);
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1942 次 |
| 最近记录: |