angular 2.0.0-rc.1 + karma: 提供路由器
angular 2.0.0-rc.1 + karma: provide Router
当测试需要 Router
的实例时,仅提供 Router
本身是不够的:
import {Router} from '@angular/router';
import {it, inject, beforeEachProviders} from '@angular/core/testing';
import {ComponentToTest} from './component.to.test';
describe('ComponentToTest', () => {
beforeEachProviders(() => [
Router,
ComponentToTest
]);
it('should call getData() on contruct', inject([Router], (router) => {
spyOn(ComponentToTest.prototype, 'getData');
expect(ComponentToTest.prototype.getData).not.toHaveBeenCalled();
let component = new ComponentToTest(router);
expect(ComponentToTest.prototype.getData).toHaveBeenCalled();
}));
});
会出现以下错误:
Error: Cannot resolve all parameters for 'Router'(?, ?, ?, ?, ?, ?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'Router' is decorated with Injectable.
但是我真的不知道怎么解决路由器。
Router
参数
_rootComponent: 对象,
_rootComponentType:类型,
在 alpha 中我们有 RootRouter
:
import {RootRouter} from 'angular2/src/router/router';
。现在已经没有任何替代品了。
_componentResolver: 组件解析器,
_urlSerializer: RouterUrlSerializer,
不知道如何提供这些。
_routerOutletMap: RouterOutletMap,
路由器本身好像已经提供了这个
_location: 位置
此参数可能仍由SpyLocation
提供:
import {SpyLocation} from '@angular/common/testing';
describe('ComponentToTest', () => {
beforeEachProviders(() => [
provide(Location, { useClass: SpyLocation }),
]);
});
导入 @angular/router/testing
并在 beforeEachProviders()
中提供 ROUTER_FAKE_PROVIDERS
beforeEachProviders(() => [
ROUTER_FAKE_PROVIDERS,
ComponentToTest
]);
另一种解决方案是使用 import {RouterTestingModule} from '@angular/router/testing';
并将 RouterTestingModule
添加到测试文件中的导入。
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
imports: [
RouterTestingModule
]
});
TestBed.compileComponents();
});
当测试需要 Router
的实例时,仅提供 Router
本身是不够的:
import {Router} from '@angular/router';
import {it, inject, beforeEachProviders} from '@angular/core/testing';
import {ComponentToTest} from './component.to.test';
describe('ComponentToTest', () => {
beforeEachProviders(() => [
Router,
ComponentToTest
]);
it('should call getData() on contruct', inject([Router], (router) => {
spyOn(ComponentToTest.prototype, 'getData');
expect(ComponentToTest.prototype.getData).not.toHaveBeenCalled();
let component = new ComponentToTest(router);
expect(ComponentToTest.prototype.getData).toHaveBeenCalled();
}));
});
会出现以下错误:
Error: Cannot resolve all parameters for 'Router'(?, ?, ?, ?, ?, ?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'Router' is decorated with Injectable.
但是我真的不知道怎么解决路由器。
Router
参数
_rootComponent: 对象,
_rootComponentType:类型,
在 alpha 中我们有 RootRouter
:
import {RootRouter} from 'angular2/src/router/router';
。现在已经没有任何替代品了。
_componentResolver: 组件解析器,
_urlSerializer: RouterUrlSerializer,
不知道如何提供这些。
_routerOutletMap: RouterOutletMap,
路由器本身好像已经提供了这个
_location: 位置
此参数可能仍由SpyLocation
提供:
import {SpyLocation} from '@angular/common/testing';
describe('ComponentToTest', () => {
beforeEachProviders(() => [
provide(Location, { useClass: SpyLocation }),
]);
});
导入 @angular/router/testing
并在 beforeEachProviders()
ROUTER_FAKE_PROVIDERS
beforeEachProviders(() => [
ROUTER_FAKE_PROVIDERS,
ComponentToTest
]);
另一种解决方案是使用 import {RouterTestingModule} from '@angular/router/testing';
并将 RouterTestingModule
添加到测试文件中的导入。
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
imports: [
RouterTestingModule
]
});
TestBed.compileComponents();
});