运行Karma测试时出现<rrouter-outlet>错误

时间:2017-08-02 05:51:47

标签: angularjs karma-jasmine angular-material2

您好我是Angular,Material和Karma的新手,我构建了一个小型应用程序并运行测试。我在app.componente.ts中添加了所有需要的模块,但是到达下面的错误。

Failed: Template parse errors:
'router-outlet' is not a known element:
1. If 'router-outlet' is an Angular component, then verify that it is part of this module.
2. If 'router-outlet' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
</div>
<div md-content>
    [ERROR ->]<router-outlet></router-outlet>
</div>

"): ng:///DynamicTestModule/AppComponent.html@6:4

以下是我的app.component中所有导入的列表。

app.component.ts的导入

import { BrowserModule } from '@angular/platform-browser';
import { RouterModule, Routes } from '@angular/router';
import {Http, HttpModule} from '@angular/http';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {FormsModule} from "@angular/forms";
import {NgModule} from "@angular/core";
import {  MdSidenavModule, MdButtonModule, MdInputModule, MdCardModule, MdMenuModule, MdToolbarModule, MdIconModule,
  MdCardContent, MdToolbar, MdToolbarRow, MdToolbarBase
} from '@angular/material'


imports: [
    BrowserModule,
    HttpModule,
    FormsModule,
    RouterModule,
    RouterModule.forRoot(appRoutes, { enableTracing: true } ),
    BrowserAnimationsModule,
    MdButtonModule,
    MdCardModule,
    MdMenuModule,
    MdToolbarModule,
    MdIconModule,
    MdInputModule
  ],

我不知道如何解决这个问题。有人可以给我一个暗示吗?

更新 这是考验。

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));

  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

app.component.html

<div layout="row">
    <div md-toolbar color="primary">
        <h1>{{ title }}</h1>
    </div> </div> <div md-content>
    <router-outlet></router-outlet> </div>

1 个答案:

答案 0 :(得分:1)

beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ], imports: [RouterTestingModule], // <--- 
    }).compileComponents();
  }));
相关问题