Angular6 当我试图在angular应用程序上执行单元测试时..但在运行测试时,我发现了以下错误

Angular6 当我试图在angular应用程序上执行单元测试时..但在运行测试时,我发现了以下错误,angular6,karma-jasmine,Angular6,Karma Jasmine,1个规格,1个故障 规格清单|故障 概览组件应创建 失败:无法解析路由器的所有参数:(,,,,,,,,,,,,,,,,?)。 错误:无法解析路由器的所有参数:(,,,,,,,,,,,,,,,,?)。 在syntaxError(http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:215:1) 在CompileMetadataResolver上。\u getDepe

1个规格,1个故障 规格清单|故障 概览组件应创建 失败:无法解析路由器的所有参数:(,,,,,,,,,,,,,,,,?)。 错误:无法解析路由器的所有参数:(,,,,,,,,,,,,,,,,?)。 在syntaxError(http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:215:1) 在CompileMetadataResolver上。\u getDependenciesMetadata(http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:10807:1) 在CompileMetadataResolver上。\u getTypeMetadata(http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:10700:1) 在CompileMetadataResolver.\u getInjectableTypeMetadata(http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:10922:1) 在CompileMetadataResolver.getProviderMetadata(http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:10931:1) 在http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:10869:1 在Array.forEach()处 在CompileMetadataResolver.\u获取ProvidersMetadata(http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:10829:1) 在CompileMetadataResolver.getNgModuleMetadata上(http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:10548:1) 在JIT编译器上。\u加载模块(http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:22567:1)

============================================


describe('OverviewComponent', () => {
  let component: OverviewComponent;
  let fixture: ComponentFixture<OverviewComponent>;
  let router : Router

 
  const fakeActivatedRoute = {
    snapshot: { data: {  } }
  } as ActivatedRoute;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports:[
        SharedModule,
        FlexLayoutModule,
       
       // RouterModule.forRoot(routes),
        RouterTestingModule
      ],
       
      declarations: [ OverviewComponent,
      FooterComponent,
      LoginComponent,
      ChangePasswordComponent,
      ForgotPasswordComponent,
      AppComponent],

      providers:[
      HttpClient,
      HttpHandler,
      DataService,
      NgxSpinnerService,
      Router,
      RouterModule,
      EmitterService,
      {provide: ActivatedRoute, 
        useValue: fakeActivatedRoute}
      ]
    })
    .compileComponents();
    

  }));

  beforeEach(() => {
    
    fixture = TestBed.createComponent(OverviewComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    
  });


  it('should create', () => {
    expect(component).toBeTruthy();
  });

 
});

描述('概览组件',()=>{
let组件:概览组件;
let夹具:组件夹具;
让路由器:路由器
常数fakeActivatedRoute={
快照:{数据:{}
}as激活路线;
beforeach(异步(()=>{
TestBed.configureTestingModule({
进口:[
共享模块,
FlexLayoutModule,
//路由模块forRoot(路由),
路由测试模块
],
声明:[概述组件,
页脚组件,
LoginComponent,
ChangePasswordComponent,
放弃密码组件,
AppComponent],
供应商:[
HttpClient,
HttpHandler,
数据服务,
NgxSpinnerService,
路由器,
路由模块,
发射塔服务,
{提供:激活的路由,
useValue:fakeActivatedRoute}
]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(概览组件);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
});
});

您需要创建模拟服务路由器

您也可以只使用RouterTestingModule和spyOn导航功能,如下所示

import { TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { Router } from '@angular/router';

import { MyModule } from './my-module';
import { MyComponent } from './my-component';

describe('something', () => {

    let fixture: ComponentFixture<LandingComponent>;
    let router: Router;

    beforeEach(() => {

        TestBed.configureTestingModule({
            imports: [
                MyModule,
                RouterTestingModule.withRoutes([]),
            ],
        }).compileComponents();

        fixture = TestBed.createComponent(MyComponent);
        router = TestBed.get(Router);

    });

    it('should navigate', () => {
        const component = fixture.componentInstance;
        const navigateSpy = spyOn(router, 'navigate');

        component.goSomewhere();
        expect(navigateSpy).toHaveBeenCalledWith(['/expectedUrl']);
    });
});
从'@angular/core/testing'导入{TestBed};
从“@angular/router/testing”导入{RouterTestingModule};
从'@angular/Router'导入{Router};
从“/MyModule”导入{MyModule};
从“/my component”导入{MyComponent};
描述(‘某物’,()=>{
let夹具:组件夹具;
让路由器:路由器;
在每个之前(()=>{
TestBed.configureTestingModule({
进口:[
我的模块,
RouterTestingModule.withRoutes([]),
],
}).compileComponents();
fixture=TestBed.createComponent(MyComponent);
路由器=测试台.get(路由器);
});
它('应该导航',()=>{
常量组件=fixture.componentInstance;
const navigateSpy=spyOn(路由器,“导航”);
goSomewhere();
expect(navigateSpy).toHaveBeenCalledWith(['/expectedUrl']);
});
});

字体:

请提供
应为概览组件创建
测试的代码。似乎您没有提供路由器请看一看code@RazorB:您的组件代码也是。如果我们不知道组件,我们如何知道单元测试行为:)