Angular 7 Testing Karma Error : Can't resolve all parameters for WebStorageService: (?)
Angular 7 Testing Karma Error : Can't resolve all parameters for WebStorageService: (?)
WebStorageService 在组件中运行良好,但在 Karma 中尝试测试时出现以下异常。
错误:无法解析 WebStorageService 的所有参数:(?)。
低于我的规格代码
import {LOCAL_STORAGE} from 'angular-webstorage-service';
import { WebStorageService } from 'angular-webstorage-service';
fdescribe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
FormsModule,
],
declarations: [
MyComponent
],
providers: [
WebStorageService
],
})
.compileComponents();
}));
有不止一种方法可以做到这一点。我建议使用 WebStorageService 的存根。这意味着服务没有被执行,但是你添加到规范中的一些模拟代码。为了测试组件,最好不要执行服务,而是提供可行的演示数据。
首先定义存根:
exort const webStoreageServiceStub = {
function1() {
return something;
}
function2() {
return something;
}
.
.
.
}
然后提供:
providers: [
{ provide: WebStorageService, useValue: webStoreageServiceStub}
]
您也可以使用
schemas: [NO_ERRORS_SCHEMA]
此处添加到您的代码中的所有内容。
exort const webStoreageServiceStub = {
function1() {
return something;
}
function2() {
return something;
}
}
describe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
FormsModule,
],
declarations: [
MyComponent
],
providers: [
{ provide: WebStorageService, useValue: webStoreageServiceStub}
],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents();
}));
WebStorageService 在组件中运行良好,但在 Karma 中尝试测试时出现以下异常。
错误:无法解析 WebStorageService 的所有参数:(?)。
低于我的规格代码
import {LOCAL_STORAGE} from 'angular-webstorage-service';
import { WebStorageService } from 'angular-webstorage-service';
fdescribe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
FormsModule,
],
declarations: [
MyComponent
],
providers: [
WebStorageService
],
})
.compileComponents();
}));
有不止一种方法可以做到这一点。我建议使用 WebStorageService 的存根。这意味着服务没有被执行,但是你添加到规范中的一些模拟代码。为了测试组件,最好不要执行服务,而是提供可行的演示数据。
首先定义存根:
exort const webStoreageServiceStub = {
function1() {
return something;
}
function2() {
return something;
}
.
.
.
}
然后提供:
providers: [
{ provide: WebStorageService, useValue: webStoreageServiceStub}
]
您也可以使用
schemas: [NO_ERRORS_SCHEMA]
此处添加到您的代码中的所有内容。
exort const webStoreageServiceStub = {
function1() {
return something;
}
function2() {
return something;
}
}
describe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
FormsModule,
],
declarations: [
MyComponent
],
providers: [
{ provide: WebStorageService, useValue: webStoreageServiceStub}
],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents();
}));