gpt4 book ai didi

Angular 2.0.0-rc.1 + 业力 : provide Router

转载 作者:太空狗 更新时间:2023-10-29 17:48:54 26 4
gpt4 key购买 nike

当测试需要 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.

但我真的不知道如何解析路由器。

路由器参数

_rootComponent: 对象,

_rootComponentType:类型,

在 alpha 中我们有 RootRouter:从“angular2/src/router/router”导入 {RootRouter};。现在已经没有任何替代品了。

_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
]);

关于 Angular 2.0.0-rc.1 + 业力 : provide Router,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37089648/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com