我刚刚升级到Angular RC 5,现在所有使用"ROUTER_指令"的组件都出现故障,出现"无法解析路由的所有参数:(?,?,?,?,,?,?,?)"当我try 对组件进行单元测试时.

import { inject, addProviders } from '@angular/core/testing';
import { ComponentFixture, TestComponentBuilder } from '@angular/core/testing';
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES, Router } from '@angular/router';

import { HomeComponent } from './home.component';
import { UserService } from '../_services/user.service';

describe('Component: Home', () => {

  beforeEach(() => {
    addProviders([HomeComponent, UserService, ROUTER_DIRECTIVES, Router]);
  });  

  it('should inject the component', inject([HomeComponent, UserService, ROUTER_DIRECTIVES, Router],
    (component: HomeComponent) => {
      expect(component).toBeTruthy();
      // expect(component.currentUser.firstname).toEqual('Jan');
    }));

完整的错误日志(log):

     Chrome 52.0.2743 (Windows 10 0.0.0)
   Error: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?).
       at new BaseException (webpack:///C:/ng/anbud/~/@angular/compiler/src/facade/exceptions.js:27:0 <- src/test.ts:2943:23)
       at CompileMetadataResolver.getDependenciesMetadata (webpack:///C:/ng/anbud/~/@angular/compiler/src/metadata_resolver.js:551:0 <- src/test.ts:24542:19)
       at CompileMetadataResolver.getTypeMetadata (webpack:///C:/ng/anbud/~/@angular/compiler/src/metadata_resolver.js:448:0 <- src/test.ts:24439:26)
       at webpack:///C:/ng/anbud/~/@angular/compiler/src/metadata_resolver.js:594:0 <- src/test.ts:24585:41
       at Array.forEach (native)
       at CompileMetadataResolver.getProvidersMetadata (webpack:///C:/ng/anbud/~/@angular/compiler/src/metadata_resolver.js:575:0 <- src/test.ts:24566:19)
       at CompileMetadataResolver.getNgModuleMetadata (webpack:///C:/ng/anbud/~/@angular/compiler/src/metadata_resolver.js:305:0 <- src/test.ts:24296:58)
       at RuntimeCompiler._compileComponents (webpack:///C:/ng/anbud/~/@angular/compiler/src/runtime_compiler.js:150:0 <- src/test.ts:37986:47)
       at RuntimeCompiler._compileModuleAndAllComponents (webpack:///C:/ng/anbud/~/@angular/compiler/src/runtime_compiler.js:78:0 <- src/test.ts:37914:37)
       at RuntimeCompiler.compileModuleAndAllComponentsSync (webpack:///C:/ng/anbud/~/@angular/compiler/src/runtime_compiler.js:52:0 <- src/test.ts:37888:21)

有没有关于如何使用路由对组件进行单元测试的 idea ?

推荐答案

终于解决了问题,就这么简单:

beforeEach(() => addProviders([
    { 
        provide: Router, 
        useClass: class { navigate = jasmine.createSpy("navigate"); }
    }]));

Typescript相关问答推荐

Angular 垫页分类和垫排序不起作用

如何在Jest中嘲笑location.back()方法调用进行Angular 组件测试?

使用新控制流的FormArray内部的Angular FormGroup

APP_INITIALIZER—TypeError:appInits不是函数

输入元素是类型变体的对象数组的正确方法是什么?'

具有继承的基于类的react 组件:呈现不能分配给基类型中的相同属性

material 表不渲染任何数据

如何提取密钥及其对应的属性类型,以供在新类型中使用?

未在DIST中正确编译TypeScrip src模块导入

React重定向参数

如何将所有props传递给React中的组件?

一个打字类型可以实现一个接口吗?

类型推断对React.ForwardRef()的引用参数无效

接受字符串或数字的排序函数

为什么发送空字段?

打字错误TS2305:模块常量没有导出的成员

VITEST警告:当前的测试运行器不支持After Each/teardown挂钩

必需的输入()参数仍然发出&没有初始值设定项&q;错误

Rxjs将省略的可观测对象合并到一个数组中

可以用任意类型实例化,该类型可能与