Angular 2:使用路由器测试组件
Angular 2: Testing components with router
我正在编写使用 routeLink 的最简单的组件:
@Component({
selector: 'memorySnippet',
templateUrl: '<div class="memory-snippet-wrapper" *ngIf="memory"
[routerLink]="['MainPanel', 'MemoryPanel', {'id' : this.memory.id}]">',
directives: [CORE_DIRECTIVES, ROUTER_DIRECTIVES]
})
export class MemorySnippetComponent {
@Input() memory: Memory;
}
当我尝试测试此组件时出现问题。我添加路由器的那一刻 link Karma 抱怨缺少提供者:
添加 Karma 要求的所有供应商后,我得到了这个:
beforeEachProviders(() => [
MemorySnippetComponent,
MEMORY_SERVICE_PROVIDERS,
ROUTER_PROVIDERS,
ApplicationRef
]);
但是当我 运行 测试时我得到这个错误:
EXCEPTION: EXCEPTION: Error during instantiation of Token RouterPrimaryComponent! (RouterLink -> Router -> RouteRegistry -> Token RouterPrimaryComponent).
ORIGINAL EXCEPTION: unimplemented
ORIGINAL STACKTRACE:
Error: unimplemented
我做错了什么??? Angular 2 (2.0.0-beta.1) 是否还没有准备好使用路由器指令测试组件?
您应该有一个 beforeEachProviders
方法,如下所示:
import {MockApplicationRef} from '@angular/core/testing';
beforeEachProviders(() => [
ROUTER_PROVIDERS,
provide(APP_BASE_HREF, {useValue: '/'}),
provide(ROUTER_PRIMARY_COMPONENT, {useValue: YourComponent}),
provide(ApplicationRef, {useClass: MockApplicationRef}
]);
MockApplicationRef
由框架提供,用于此类测试。
对于带有新路由器的 RC4,现在使用 ...
beforeEach(() => addProviders([
APP_ROUTER_PROVIDERS, // must be first
{provide: APP_BASE_HREF, useValue: '/'}, // must be second
{provide: ActivatedRoute, useClass: Mock},
{provide: Router, useClass: Mock}
]));
使用这种方法的一个 github 项目是...
我正在编写使用 routeLink 的最简单的组件:
@Component({
selector: 'memorySnippet',
templateUrl: '<div class="memory-snippet-wrapper" *ngIf="memory"
[routerLink]="['MainPanel', 'MemoryPanel', {'id' : this.memory.id}]">',
directives: [CORE_DIRECTIVES, ROUTER_DIRECTIVES]
})
export class MemorySnippetComponent {
@Input() memory: Memory;
}
当我尝试测试此组件时出现问题。我添加路由器的那一刻 link Karma 抱怨缺少提供者:
添加 Karma 要求的所有供应商后,我得到了这个:
beforeEachProviders(() => [
MemorySnippetComponent,
MEMORY_SERVICE_PROVIDERS,
ROUTER_PROVIDERS,
ApplicationRef
]);
但是当我 运行 测试时我得到这个错误:
EXCEPTION: EXCEPTION: Error during instantiation of Token RouterPrimaryComponent! (RouterLink -> Router -> RouteRegistry -> Token RouterPrimaryComponent).
ORIGINAL EXCEPTION: unimplemented
ORIGINAL STACKTRACE: Error: unimplemented
我做错了什么??? Angular 2 (2.0.0-beta.1) 是否还没有准备好使用路由器指令测试组件?
您应该有一个 beforeEachProviders
方法,如下所示:
import {MockApplicationRef} from '@angular/core/testing';
beforeEachProviders(() => [
ROUTER_PROVIDERS,
provide(APP_BASE_HREF, {useValue: '/'}),
provide(ROUTER_PRIMARY_COMPONENT, {useValue: YourComponent}),
provide(ApplicationRef, {useClass: MockApplicationRef}
]);
MockApplicationRef
由框架提供,用于此类测试。
对于带有新路由器的 RC4,现在使用 ...
beforeEach(() => addProviders([
APP_ROUTER_PROVIDERS, // must be first
{provide: APP_BASE_HREF, useValue: '/'}, // must be second
{provide: ActivatedRoute, useClass: Mock},
{provide: Router, useClass: Mock}
]));
使用这种方法的一个 github 项目是...