Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度7测试业力错误:Can';t解析WebStorage服务的所有参数:(?)_Angular_Angular7_Karma Jasmine - Fatal编程技术网

Angular 角度7测试业力错误:Can';t解析WebStorage服务的所有参数:(?)

Angular 角度7测试业力错误:Can';t解析WebStorage服务的所有参数:(?),angular,angular7,karma-jasmine,Angular,Angular7,Karma Jasmine,WebStorage服务在组件中运行良好,但当尝试在Karma中进行测试时,会出现以下异常 错误:无法解析WebStorage Service:(?)的所有参数。 低于我的规格代码 import {LOCAL_STORAGE} from 'angular-webstorage-service'; import { WebStorageService } from 'angular-webstorage-service'; fdescribe('MyComponent', () => {

WebStorage服务在组件中运行良好,但当尝试在Karma中进行测试时,会出现以下异常

错误:无法解析WebStorage Service:(?)的所有参数。

低于我的规格代码

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();
  }));
从“角度Web存储服务”导入{LOCAL_STORAGE};
从'angular webstorage service'导入{webstorage service};
fdescribe('MyComponent',()=>{
let组分:MyComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
进口:[
路由器测试模块,
FormsModule,
],
声明:[
真菌成分
],
提供者:[
网络存储服务
],
})
.compileComponents();
}));

实现这一点的方法不止一种。我建议使用WebStorage服务的存根。这意味着不执行该服务,而是将一些模拟代码添加到规范中。对于测试组件,最好不要执行该服务,而是提供可行的演示数据

首先定义存根:

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();
}));
exort const webStoreageServiceStub={
职能1(){
归还某物;
}
职能2(){
归还某物;
}
}
描述('MyComponent',()=>{
let组分:MyComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
进口:[
路由器测试模块,
FormsModule,
],
声明:[
真菌成分
],
提供者:[
{提供:WebStorage服务,useValue:webStoreageServiceStub}
],
架构:[无错误\u架构]
})
.compileComponents();
}));

能否添加组件html和ts文件代码的代码,并按照建议进行更新。抛出以下错误。失败:StaticInjectorError(DynamicTestModule)[MyComponent->InjectionToken LOCAL_STORAGE]:StaticInjectorError(Platform:core)[MyComponent->InjectionToken LOCAL_STORAGE]:NullInjectorError:没有InjectionToken本地存储的提供程序!本地存储是否在测试代码中的任何位置使用?如果没有,请删除导入。我的组件构造函数(@Injection(LOCAL_STORAGE)专用存储:WebStorage Service)我不确定,但我认为您还必须为本地_存储创建存根。即使在规范中删除了本地_存储导入后,也会抛出相同的错误。可能需要在规范文件中添加“提供程序”部分