Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.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 由于';路由器出口';_Angular_Routing_Routes_Karma Jasmine - Fatal编程技术网

Angular 由于';路由器出口';

Angular 由于';路由器出口';,angular,routing,routes,karma-jasmine,Angular,Routing,Routes,Karma Jasmine,我正在使用“ng测试”运行我的测试用例。它在路由级别失败。请找到截图 位于app.component.html 如何在app.component.spec.ts中添加路由器测试以通过测试用例?我完成了以下步骤 1) 在根文件夹的测试文件夹中创建router-stubs.ts,代码如下 import { Directive, Input } from '@angular/core'; @Directive({ selector: '[routerLink]', host: {

我正在使用“ng测试”运行我的测试用例。它在路由级别失败。请找到截图


位于
app.component.html


如何在app.component.spec.ts中添加路由器测试以通过测试用例?

我完成了以下步骤

1) 在根文件夹的测试文件夹中创建router-stubs.ts,代码如下

import { Directive, Input } from '@angular/core';

@Directive({
  selector: '[routerLink]',
  host: {
    '(click)': 'onClick()'
  }
})
export class RouterLinkStubDirective {
  @Input('routerLink') linkParams: any;
  navigatedTo: any = null;

  onClick() {
    this.navigatedTo = this.linkParams;
  }
}
2) 将以下代码添加到app.component.spec.ts

import { NO_ERRORS_SCHEMA } from '@angular/core';
import { RouterLinkStubDirective } from '../testing/router-stubs'; 

describe('AppComponent', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [ AppComponent, RouterLinkStubDirective ],
      schemas:      [ NO_ERRORS_SCHEMA ]
    })
    .compileComponents()
    .then(() => {
      let fixture = TestBed.createComponent(AppComponent);
      let comp    = fixture.componentInstance;
      // trigger initial data binding
      fixture.detectChanges();
    });   
  });

它修复了错误“路由器出口不是已知元素”

我执行了以下步骤,问题得到了修复

1) 在根文件夹的测试文件夹中创建router-stubs.ts,代码如下

import { Directive, Input } from '@angular/core';

@Directive({
  selector: '[routerLink]',
  host: {
    '(click)': 'onClick()'
  }
})
export class RouterLinkStubDirective {
  @Input('routerLink') linkParams: any;
  navigatedTo: any = null;

  onClick() {
    this.navigatedTo = this.linkParams;
  }
}
2) 将以下代码添加到app.component.spec.ts

import { NO_ERRORS_SCHEMA } from '@angular/core';
import { RouterLinkStubDirective } from '../testing/router-stubs'; 

describe('AppComponent', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [ AppComponent, RouterLinkStubDirective ],
      schemas:      [ NO_ERRORS_SCHEMA ]
    })
    .compileComponents()
    .then(() => {
      let fixture = TestBed.createComponent(AppComponent);
      let comp    = fixture.componentInstance;
      // trigger initial data binding
      fixture.detectChanges();
    });   
  });

它修复了错误“路由器出口不是已知元素”

除非我弄错了,否则您不需要为此创建自己的路由器存根。 你所要做的就是导入角度测试路由器模块,而不是真正的一个或根本没有

在您的TestBed.configureTestingModule中

  imports: [RouterTestingModule]

除非我弄错了,否则您不需要为此创建自己的路由器存根。 你所要做的就是导入角度测试路由器模块,而不是真正的一个或根本没有

在您的TestBed.configureTestingModule中

  imports: [RouterTestingModule]