简体   繁体   English

使用 Karma 进行测试-无法绑定到“routerLink”,因为它不是

[英]Testing with Karma- Can't bind to 'routerLink' since it isn't a known property of

I am new to testing with Karma/Jasmine in my Angular 6 app and could use some help with one of my most common failures.我是在我的 Angular 6 应用程序中使用 Karma/Jasmine 进行测试的新手,可以使用一些帮助来解决我最常见的失败之一。

It's the use and configuration of 'RouterLink' in my template.这是我模板中'RouterLink'的使用和配置。 I have no error running the app, but upon running tests, I receive the error:运行应用程序没有错误,但在运行测试时,我收到错误:

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

I can't quite configure my spec file correctly.我无法正确配置我的规范文件。 Any thoughts on how I can fix my configurations to fix that error when I run my tests?关于在运行测试时如何修复配置以修复该错误的任何想法? I'm not sure if it's an issue with my app.module file, my component, or test file.我不确定这是否是我的 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();
  });
});

This is the line in the html that it's catching:这是它捕获的 html 中的行:

<a [routerLink]="['/sites', current_mp.siteId]" ><i class='fa fa-ban'></i> Cancel</a>

My component looks like:我的组件看起来像:

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 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!

You should add RouterTestingModule to your imports in your spec file.您应该将 RouterTestingModule 添加到您的规范文件中的导入中。 That should allow Angular to recognize your routerLink directive.这应该允许 Angular 识别您的 routerLink 指令。 Also clear the providers array.还要清除提供者数组。

Please refer to this tutorial for more information: https://codecraft.tv/courses/angular/unit-testing/routing/更多信息请参考本教程: https : //codecraft.tv/courses/angular/unit-testing/routing/

Also if are not interested in "Testing" the routing inside the component you can simply add NO_ERROR_SCHEMA to your configureTestingModule method.此外,如果对“测试”组件内部的路由不感兴趣,您可以简单地将NO_ERROR_SCHEMA添加到您的 configureTestingModule 方法中。

schemas: [NO_ERRORS_SCHEMA]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 许多失败:模板解析错误:无法绑定到“routerLink”,因为它不是“a”的已知属性。 Karma 中的错误 - Lots of Failed: Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a'. errors in Karma 无法绑定到“routerLink”,因为它不是已知属性 - Can't bind to 'routerLink' since it isn't a known property Angular 7 无法绑定到“routerlink”,因为它不是“a”的已知属性 - Angular 7 and can't bind to 'routerlink' since it isn't a known property of 'a' Angular2 单元测试错误:无法绑定到“routerLink”,因为它不是“a”的已知属性 - Angular2 unit testing error: Can't bind to 'routerLink' since it isn't a known property of 'a' 无法绑定到“routerLink”,因为它是已知属性 - Can't bind to 'routerLink' since it is a known property 无法绑定到“测试”,因为它不是“输入”的已知属性 - Can't bind to 'testing' since it isn't a known property of 'input' 尽管在app.module中声明了RouterModule,但由于它不是&#39;a&#39;的已知属性,因此无法绑定到&#39;routerlink&#39; - Can't bind to 'routerlink' since it isn't a known property of 'a' despite declaring RouterModule in app.module 模板解析错误:无法绑定到&#39;routerLink&#39;,因为它不是&#39;a&#39;的已知属性 - Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a' RC4 - 无法绑定到&#39;routerLink&#39;,因为它不是已知的本机属性 - RC4 - Can't bind to 'routerLink' since it isn't a known native property 从“是2个模块的声明的一部分”开始,得到“无法绑定到&#39;routerLink&#39;,因为它不是&#39;a&#39;的已知属性” - Started from 'is part of the declarations of 2 modules', got “Can't bind to 'routerLink' since it isn't a known property of 'a'”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM