【问题标题】:Testing with Karma- Can't bind to 'routerLink' since it isn't a known property of使用 Karma 进行测试 - 无法绑定到“routerLink”,因为它不是
【发布时间】:2019-07-15 04:37:58
【问题描述】:

我是在我的 Angular 6 应用程序中使用 Karma/Jasmine 测试的新手,并且可以在我最常见的故障之一中使用一些帮助。

这是我的模板中'RouterLink'的使用和配置。我运行应用程序没有错误,但在运行测试时,我收到错误:

Can't bind to 'routerLink' since it isn't a known property of 'a'.

我无法正确配置我的规范文件。关于在运行测试时如何修复配置以修复该错误的任何想法?我不确定我的 app.module 文件、组件或测试文件是否有问题。

import { NavigationComponent } from '../navigation/navigation.component';
import { EditMonitoringPointComponent } from './edit-monitoring-point.component';
import { FormsModule, NgModel } from '@angular/forms';
import { RouterLink, RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';

describe('EditMonitoringPointComponent', () => {
  let component: EditMonitoringPointComponent;
  let fixture: ComponentFixture<EditMonitoringPointComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ FormsModule],
      declarations: [ EditMonitoringPointComponent, NavigationComponent],
      providers: [ RouterModule, RouterLink],
      // directives: [ROUTER_DIRECTIVES]

    })
    .compileComponents();
  }));

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

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

这是它正在捕获的 html 中的行:

&lt;a [routerLink]="['/sites', current_mp.siteId]" &gt;&lt;i class='fa fa-ban'&gt;&lt;/i&gt; Cancel&lt;/a&gt;

我的组件看起来像:

import { ActivatedRoute, ParamMap, Router, RouterModule, RouterLink } from '@angular/router';
import { SiteService } from '../../services/site.service'
import { MonitoringPointService } from '../../services/monitoring-point.service'
import { AuthService } from '../../services/auth.service'
import { DeviceService } from '../../services/device.service';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';

@Component({
  selector: 'app-edit-monitoring-point',
  templateUrl: './edit-monitoring-point.component.html',
  styleUrls: ['./edit-monitoring-point.component.css']
})

export class EditMonitoringPointComponent implements OnInit {
//declare variables
}

constructor(private router: Router, private monitoringPointService: MonitoringPointService, public dialog: MatDialog, private deviceService: DeviceService, public siteService: SiteService, private route: ActivatedRoute, private SiteService: SiteService, private authService: AuthService) { }

ngOnInit() {
...
}

app.module.ts

import { NgModule } from '@angular/core';
import { 
RouterModule}  from '@angular/router';
import { AppRoutingModule, routingComponents } from './app-routing.module';
import { FormsModule } from '@angular/forms';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent,
    routingComponents],
  imports: [
    RouterModule,
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    HttpClientModule,],
  providers: [
      {provide: HTTP_INTERCEPTORS,
      useClass: UnauthorizedResponseInterceptor,
      multi: true}]
})
export class AppModule { }
platformBrowserDynamic().bootstrapModule(AppModule);

Thank you so much!

【问题讨论】:

    标签: angular typescript testing karma-jasmine


    【解决方案1】:

    您应该将 RouterTestingModule 添加到您的规范文件中的导入中。 这应该允许 Angular 识别您的 routerLink 指令。 同时清除提供者数组。

    请参阅本教程了解更多信息: https://codecraft.tv/courses/angular/unit-testing/routing/

    此外,如果对“测试”组件内的路由不感兴趣,您可以简单地将 NO_ERROR_SCHEMA 添加到您的 configureTestingModule 方法中。

    schemas: [NO_ERRORS_SCHEMA]
    

    【讨论】:

    • 这太好了,谢谢。我没有意识到有一个单独的路由模块用于测试
    • NO_ERRORS_SCHEMA 非常危险,因为它可以隐藏其他副作用,从而破坏您的测试
    猜你喜欢
    • 2020-12-28
    • 2017-06-21
    • 1970-01-01
    • 2018-09-02
    • 2017-02-05
    • 2019-04-08
    • 2016-09-05
    • 2017-04-06
    相关资源
    最近更新 更多