为使用router-outlet的组件编写单元测试

时间:2017-06-11 08:28:44

标签: angular unit-testing typescript router-outlet

任何人都知道为什么我在尝试实例化使用<router-outlet>的组件时遇到错误?

console.log('then');没有被调用,所以编译可能有问题吗?

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html'
})
export class AppComponent { }

app.component.html

<a id="newPage" routerLink="/new-page">New page</a>
<router-outlet></router-outlet>

APP-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { PageDetailsComponent } from './new-page/page-details.component';

export const routes: Routes = [
  { path: 'new-page', component: PageDetailsComponent }
];
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

app.component.spec.ts

import { async, TestBed } from '@angular/core/testing';
import { routes } from './app-routing.module';
import { RouterTestingModule } from '@angular/router/testing';

import { AppComponent } from './app.component';
import { PageDetailsComponent } from './page-details/page-details.component';

describe('AppComponent', () => {

  let component: AppComponent;

  beforeEach(async(() => {
    console.log('as');
    TestBed.configureTestingModule({
      imports: [RouterTestingModule.withRoutes(routes)],
      declarations: [AppComponent, PageDetailsComponent]
    }).compileComponents().then(() => {
      console.log('then');
      const fixture = TestBed.createComponent(AppComponent);
      component = fixture.componentInstance;
    });
  }));

  it('should create an instance of AppComponent', () => {
    expect(true).toBe(true);
  });
});

错误我看到了:

11 06 2017 09:24:58.055:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket pAXmtnKm0A2IT8ZcAAAA with id 17069837
LOG: 'as'
PhantomJS 2.1.1 (Linux 0.0.0) AppComponent should create an instance of AppComponent FAILED
    invokeTask@/tmp/karma-typescript-bundle-10912v4sMPp2rIrzg.js:793:36
    onInvokeTask@/tmp/karma-typescript-bundle-10912v4sMPp2rIrzg.js:2908:49
    invokeTask@/tmp/karma-typescript-bundle-10912v4sMPp2rIrzg.js:792:48
    runTask@/tmp/karma-typescript-bundle-10912v4sMPp2rIrzg.js:560:57
    drainMicroTaskQueue@/tmp/karma-typescript-bundle-10912v4sMPp2rIrzg.js:953:42
    run@/tmp/karma-typescript-bundle-10912v4sMPp2rIrzg.js:20781:29
    /tmp/karma-typescript-bundle-10912v4sMPp2rIrzg.js:20794:31
    flush@/tmp/karma-typescript-bundle-10912v4sMPp2rIrzg.js:20643:11
    resolvePromise@/tmp/karma-typescript-bundle-10912v4sMPp2rIrzg.js:1140:78
    resolvePromise@/tmp/karma-typescript-bundle-10912v4sMPp2rIrzg.js:1110:31
    /tmp/karma-typescript-bundle-10912v4sMPp2rIrzg.js:1187:31
PhantomJS 2.1.1 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.068 secs / 0.082 secs)

0 个答案:

没有答案