简体   繁体   English

组件未定义没有路由配置又如何配置Angular 2路由器进行单元测试?

[英]Component undefined has no route config aka how to configure Angular 2 router for unit test?

I'm working on setting up a specification for routing with Angular2. 我正在设置一个使用Angular2进行路由的规范。 This is the app component: 这是应用程序组件:

import {Component, View} from 'angular2/core';

import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {Search} from './search/search';
import {SearchResults} from './search-results/search-results';

@Component({
     selector: 'my-app'
})
@View({
    template: `<div>
    <router-outlet></router-outlet>
     </div>`,
    directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
    {path: '/search', name: 'Search', component: Search, useAsDefault: true},
    {path: '/search-results', name: 'SearchResults', component: SearchResults}
])
export class App {
}

This is the search component containing the navigation: 这是包含导航的搜索组件:

import {Component} from 'angular2/core';
import {Router} from "angular2/router";

@Component({
     template: '<div></div>'
})
export class Search {
    constructor(public router: Router) {}

    onSelect(station:Station):void {
        this.router.navigate(['SearchResults']);
    }
}

The search results component: import {Component} from "angular2/core"; 搜索结果组件:从“angular2 / core”导入{Component};

@Component({
    template: '<div></div>'
})
export class SearchResults {
    constructor() {
    }
}

This is the specification: 这是规范:

import {
    it,
    inject,
    describe,
    beforeEachProviders,
    MockApplicationRef
} from 'angular2/testing';

import {Component, provide, ApplicationRef} from 'angular2/core';

import {
    APP_BASE_HREF, ROUTER_PRIMARY_COMPONENT, ROUTER_PROVIDERS, ROUTER_DIRECTIVES
} from "angular2/router";
import {Search} from "./search";
import {App} from "../app";
import {SearchResults} from "../search-results/search-results";

import {bootstrap}    from 'angular2/platform/browser';
import {Http, BaseRequestOptions} from "angular2/http";
import {MockBackend} from "angular2/src/http/backends/mock_backend";


describe('Search', () => {

// provide our implementations or mocks to the dependency injector
beforeEachProviders(() => [
    ROUTER_PROVIDERS,
    ROUTER_DIRECTIVES,
    provide(ROUTER_PRIMARY_COMPONENT, {useClass: App}),
    provide(APP_BASE_HREF, {useValue : '/'}),
    provide(ApplicationRef, {useClass: MockApplicationRef}),
    Search
]);

it('navigates', inject([Search], (search) => {
    search.onSelect(CHOICE);
    expect(search.router.navigating).toBe(true);
}));
});

The production code works, but the specification doesn't. 生产代码有效,但规范没有。 Apparently there's still something missing in the router setup, because I get the following error: "Component undefined has no route config." 显然路由器设置中仍然缺少一些东西,因为我收到以下错误:“组件未定义没有路由配置。” I debugged into the Angular2 (beta.1) code and this exception will be thrown on line 2407 of router.dev.js. 我调试了Angular2(beta.1)代码,这个异常将在router.dev.js的第2407行抛出。 It means that there's no component recognizer assigned to this component, but I don't know how to resolve it. 这意味着没有为此组件分配组件识别器,但我不知道如何解决它。

I use the following provider function: 我使用以下提供程序函数:

import {AppComponent} from "../components/app/app-component";
import {Router, ROUTER_PRIMARY_COMPONENT, RouteRegistry} from "angular2/router";
import {RootRouter} from "angular2/src/router/router";
import {SpyLocation} from "angular2/src/mock/location_mock";
import {Location} from "angular2/src/router/location/location";

export function provideMockRouter():any[] {
    return [
        RouteRegistry,
        provide(Location, {useClass: SpyLocation}),
        provide(ROUTER_PRIMARY_COMPONENT, {useValue: AppComponent}),
        provide(Router, {useClass: RootRouter}),
    ];
}

Which I include: 我包括:

beforeEachProviders(() => [
    provideMockRouter(),
    ...
]);

And use as follows: 使用方法如下:

it('navigates', testAsyncAwait(async() => {
    spyOn(router, 'navigate').and.callThrough();
    await component.call();
    expect(router.navigate).toHaveBeenCalledWith(['TargetComponent']);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM