我正在尝试使用与RC1合作的单元测试与新的"新的"路由器。如何在3.0.0-alpha.8
上实现此目的?
我的依赖项:
"@angular/common": "2.0.0-rc.3",
"@angular/compiler": "2.0.0-rc.3",
"@angular/core": "2.0.0-rc.3",
"@angular/forms": "0.2.0",
"@angular/platform-browser": "2.0.0-rc.3",
"@angular/platform-browser-dynamic": "2.0.0-rc.3",
"@angular/router": "3.0.0-beta.2",
我想测试的组件使用routerLink
指令:
import {ROUTER_DIRECTIVES} from '@angular/router';
@Component({
selector: 'app-topmenu',
template: require('./app-topmenu.tpl.html'),
directives: [ROUTER_DIRECTIVES]
})
export class TopMenu {
<nav class="app-top-menu">
<a *ngFor="let link of links" [routerLink]="link.route">{{link.text}}</a>
</nav>
以前,使用rc1,我使用类似的东西对我的组件进行单元测试:
import {Location} from '@angular/common';
import {SpyLocation} from '@angular/common/testing';
import {Router, RouteRegistry, ROUTER_DIRECTIVES, ROUTER_PRIMARY_COMPONENT} from '@angular/router-deprecated';
import {RootRouter} from '@angular/router-deprecated/src/router';
describe('Router link capabilities', () => {
beforeEachProviders(() => [
RouteRegistry,
{ provide: Location, useClass: SpyLocation },
{ provide: ROUTER_PRIMARY_COMPONENT, useValue: TestComponent },
{ provide: Router, useClass: RootRouter }
]);
it('creates routerLinks with the expected URLs', fakeAsync(
inject([TestComponentBuilder, Location], (tcb: TestComponentBuilder, spyLocation: SpyLocation) => {
tcb.overrideTemplate(TestComponent, `
<app-top-menu [links]='[
{ text: "A", route: ["/TestA/TestB/TestC"] },
{ text: "B", route: ["/TestA", "TestB", "TestC"] }
]'></app-top-menu>
<router-outlet></router-outlet>
`).createAsync(TestComponent)
.then((fixture: ComponentFixture<TestComponent>) => {
fixture.detectChanges();
spyLocation.simulateUrlPop('url-a/url-b/url-c');
tick();
fixture.detectChanges();
let nativeLinks: HTMLAnchorElement[] = fixture.nativeElement.querySelectorAll('a');
expect(nativeLinks.length).toBe(2);
expect(nativeLinks[0].textContent).toBe('A');
expect(nativeLinks[1].textContent).toBe('B');
});
})
));
});
当我尝试将routerLink
导入从@angular/router-deprecated
切换为ROUTER_DIRECTIVES
导入@angular/router
时,我收到错误消息:
ORIGINAL EXCEPTION: Platforms have to be created via `createPlatform`!
我可以在网上找到有关测试&#34;新新&#34;的所有文档。路由器和 createPlatform 消息是指提供ROUTER_FAKE_PROVIDERS
,它似乎包含在rc2中,但与rc3一起使用:
import {ROUTER_FAKE_PROVIDERS} from '@angular/router/testing';
// ^ [ts] Cannot find module '@angular/router/testing'.
搜索.d.ts
文件夹中已编译的node_modules/@angular/router
文件,我也没有找到任何对测试/模拟/假货的引用。
有没有人迁移到rc3并让它正常工作?
答案 0 :(得分:3)
要测试RC3(3.0.0-alpha。*)路由器,您需要做的事情与先前版本中路由器的设置方式不同。
您需要像这样定义RouterConfig
:
import {provideRouter, RouterConfig} from '@angular/router';
export const APP_ROUTES : RouterConfig = [
{path: '', component: AppComponent},
// more paths
];
export const APP_ROUTE_PROVIDERS = [
provideRouter(APP_ROUTES)
];
然后在你的测试文件中
import {ActivatedRoute, RouterConfig, Router} from '@angular/router';
class MockRouter { createUrlTree() {} }
class MockActivatedRoute { }
beforeEachProviders(() => [
provide(Router, { useClass: MockRouter }),
provide(ActivatedRoute, { useClass: MockActivatedRoute }),
]);
describe(){
// etc
}
您的路由器现在可以测试。
RC4的:
import {ActivatedRoute, RouterConfig, Router} from '@angular/router';
class MockRouter { createUrlTree() {} }
class MockActivatedRoute { }
describe(){
beforeEach(() => [
addProviders([
provide(Router, { useClass: MockRouter }),
provide(ActivatedRoute, { useClass: MockActivatedRoute }),
]);
]);
//etc
}