[英]Lots of Failed: Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a'. errors in Karma
Failed: Template parse errors:
Can't bind to 'routerLink' since it isn't a known property of 'a'. ("nd">
<img width="25" src="../assets/images/app_logo.jpg">
<a class="navbar-brand" [ERROR ->][routerLink]="['/avior/dashboard']"> === Application ===</a>
</a>
"): ng:///DynamicTestModule/AppComponent.html@7:27
...
NullInjectorError: StaticInjectorError[AviorBackendService]:
NullInjectorError: No provider for AviorBackendService!
...
Failed: Unexpected pipe 'SearchPipe' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.
...
Expected undefined to be truthy.
The same error goes over and over again for every a anchor with [routerLink] inside of it...对于每个包含 [routerLink] 的锚点,同样的错误一遍又一遍......
I tried importing RouterModule into app.component.ts AND app.module.ts, but it does not seem to help.我尝试将 RouterModule 导入 app.component.ts AND app.module.ts,但似乎没有帮助。 I also imported it into every .ts file where the routerLink is used but that didn't help either.
我还将它导入到使用 routerLink 的每个 .ts 文件中,但这也无济于事。
I tried importing import { RouterTestingModule } from '@angular/router/testing';
我尝试
import { RouterTestingModule } from '@angular/router/testing';
导入import { RouterTestingModule } from '@angular/router/testing';
in the spec.ts and that didn't help either but I think it's somewhere in the right way.在 spec.ts 中,这也无济于事,但我认为这是正确的方式。
PS I haven't changed the .spec.ts files with a single exception. PS 我没有更改 .spec.ts 文件,只有一个例外。
UPDATE更新
My dashboard component spec.ts:我的仪表板组件规格:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DashboardComponent } from './dashboard.component';
describe('DashboardComponent', () => {
let component: DashboardComponent;
let fixture: ComponentFixture<DashboardComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ DashboardComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(DashboardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
My Login Component spec.ts:我的登录组件规范:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { LoginComponent } from './login.component';
describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LoginComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
As you can see they're default tests.如您所见,它们是默认测试。
At AppComponent.html@7:27 the problem was indeed nested a anchors:在 AppComponent.html@7:27 问题确实嵌套了一个锚点:
<a class="navbar-brand">
<img width="25" src="../assets/images/app_logo.jpg">
<a class="navbar-brand" [routerLink]="['/avior/dashboard']"> === Application ===</a>
</a>
I changed the outer a anchor to div, but the other errors still persist.我将外部 a 锚点更改为 div,但其他错误仍然存在。
The real RouterLinkDirective
is quite complicated and entangled with other components and directives of the RouterModule
.真正的
RouterLinkDirective
非常复杂,并且与RouterModule
其他组件和指令纠缠在一起。 It requires challenging setup to mock and use in tests.它需要具有挑战性的设置来模拟和在测试中使用。
The RouterLinkDirectiveStub
in this sample code replaces the real directive with an alternative version designed to validate the kind of anchor tag wiring seen in the AppComponent template
(For example).此示例代码中的
RouterLinkDirectiveStub
用替代版本替换真实指令,该版本旨在验证AppComponent template
(例如)中看到的锚标记接线类型。
For more information, visit the angular testing docs有关更多信息,请访问角度测试文档
For those interested the solution was the following - I needed to import RouterTestingModule (and also ReactiveFormsModule and HttpClientTestingModule) and in imports specify the routes like the following:对于那些感兴趣的人,解决方案如下 - 我需要导入 RouterTestingModule(以及 ReactiveFormsModule 和 HttpClientTestingModule)并在导入中指定如下路由:
imports: [ ReactiveFormsModule, HttpClientTestingModule, RouterTestingModule.withRoutes(
[{path: '', redirectTo: 'avior/login', pathMatch: 'full'},
{path: 'avior', loadChildren: () => import('../avior.module').then(m => m.AviorModule)},
{path: 'avior/*', redirectTo: '/avior'},
{path: 'carinae', loadChildren: () => import('../../carinae/carinae.module').then(m => m.CarinaeModule)},
{path: 'carinae/*', redirectTo: '/carinae'},
{path: '**', redirectTo: '/avior/404'}]
) ],
For为了
NullInjectorError: StaticInjectorError[AviorBackendService]:
NullInjectorError: No provider for AviorBackendService!
I needed to declare the BackendService as a provider and also import HttpClientTestingModule.我需要将 BackendService 声明为提供者并导入 HttpClientTestingModule。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.