我正在使用RC3
. 我正在实施Angular2
此处记录的新路由器:https ://angular.io/docs/ts/latest/guide/router.html
一切正常,但我在单元测试中遇到问题。具体来说,我无法将Angular2
服务注入到我的单元测试中。
我的相关组件代码是:
import {Component} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
@Component({
templateUrl: ...
styleUrls: ...
})
export class Route1DetailComponent {
constructor(private route:ActivatedRoute) {
console.log(route);
}
}
我的单元测试看起来像:
import {
expect, it, iit, xit,
describe, ddescribe, xdescribe,
beforeEach, beforeEachProviders, withProviders,
async, inject
} from '@angular/core/testing';
import {ActivatedRoute} from '@angular/router';
import {Route1DetailComponent} from './route1-detail.component';
import {TestComponentBuilder} from '@angular/compiler/testing';
describe('route1-detail.component.ts', () => {
beforeEachProviders(() => [
{provide: ActivatedRoute, useClass: ActivatedRoute}
]);
it('should instantiate component',
async(inject([TestComponentBuilder, ActivatedRoute], (tcb:TestComponentBuilder, ar: ActivatedRoute) => {
tcb.createAsync(Route1DetailComponent).then((fixture) => {
expect(fixture.componentInstance instanceof Route1DetailComponent).toBe(true, 'should create Route1DetailComponent');
console.log(ar);
});
})));
});
“应该实例化组件”单元测试失败。错误是:
无法解析“ActivatedRoute”的所有参数(?、?、?、?、?)。确保所有参数都用 Inject 修饰或具有有效的类型注释,并且 'ActivatedRoute' 用 Injectable 修饰。
我如何让这个工作?
当我不注入时,ActivatedRoute
一切正常。
谢谢。