未定义的组件没有路由配置,又如何为单元测试配置Angular 2路由器? [英] Component undefined has no route config aka how to configure Angular 2 router for unit test?
问题描述
我正在为Angular2建立路由规范. 这是应用程序组件:
I'm working on setting up a specification for routing with 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']);
}
}
搜索结果部分: 从"angular2/core"导入{Component};
The search results component: import {Component} from "angular2/core";
@Component({
template: '<div></div>'
})
export class SearchResults {
constructor() {
}
}
这是规范:
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);
}));
});
生产代码有效,但规范无效.显然,路由器设置中仍然缺少一些东西,因为我收到以下错误消息: 未定义的组件没有路由配置." 我调试了Angular2(beta.1)代码,此异常将引发在router.dev.js的2407行上.这意味着没有分配给该组件的组件识别器,但是我不知道如何解决它.
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. 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}),
];
}
我包括:
beforeEachProviders(() => [
provideMockRouter(),
...
]);
并使用如下:
it('navigates', testAsyncAwait(async() => {
spyOn(router, 'navigate').and.callThrough();
await component.call();
expect(router.navigate).toHaveBeenCalledWith(['TargetComponent']);
}
这篇关于未定义的组件没有路由配置,又如何为单元测试配置Angular 2路由器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!