Angular 手动注入路由器后的单体测试喷油器(APP_初始化器)

Angular 手动注入路由器后的单体测试喷油器(APP_初始化器),angular,unit-testing,dependency-injection,karma-jasmine,angular-router,Angular,Unit Testing,Dependency Injection,Karma Jasmine,Angular Router,在app.module.ts中,我有以下内容: {provide: APP_INITIALIZER, useFactory: appServiceFactory, multi: true, deps: [AppService]} 我试图在AppService中添加Router依赖项,但出现以下错误: Cannot instantiate cyclic dependency! ApplicationRef... ERROR LOG: 'Unhandled Promise rejection:'

app.module.ts
中,我有以下内容:

{provide: APP_INITIALIZER, useFactory: appServiceFactory, multi: true, deps: [AppService]}
我试图在
AppService
中添加
Router
依赖项,但出现以下错误:

Cannot instantiate cyclic dependency! ApplicationRef...
ERROR LOG: 'Unhandled Promise rejection:', 'Cannot read property 'then' of undefined', '; Zone:', 'ProxyZone', '; Task:', 'jasmine.onComplete', '; Value:', TypeError: Cannot read property 'then' of undefined
TypeError: Cannot read property 'then' of undefined
我向我的
AppService
构造函数添加了
Injector
,并手动添加了
Router

private get router() {
    return this.injector.get(Router);
}
let injector: Injector;

const routerStub = {navigate: jasmine.createSpy('navigate')};

beforeEach(() => {
    TestBed.configureTestingModule({
        imports: [],
        providers: [
            AppService,
            ...,
            Injector,
            {provide: Router, useValue: routerStub}
        ]
    });

    injector = TestBed.get(Injector);

    spyOn(injector, 'get').and.returnValue(routerStub);
});
我成功地这样使用它:

this.router.navigate([ routePath ], {queryParams: {type: paramType}})
    .then(() => {
        return resolve();
    });
===

为了进行测试,我向提供者添加了
注入器
,并将
路由器

private get router() {
    return this.injector.get(Router);
}
let injector: Injector;

const routerStub = {navigate: jasmine.createSpy('navigate')};

beforeEach(() => {
    TestBed.configureTestingModule({
        imports: [],
        providers: [
            AppService,
            ...,
            Injector,
            {provide: Router, useValue: routerStub}
        ]
    });

    injector = TestBed.get(Injector);

    spyOn(injector, 'get').and.returnValue(routerStub);
});
我得到以下错误:

Cannot instantiate cyclic dependency! ApplicationRef...
ERROR LOG: 'Unhandled Promise rejection:', 'Cannot read property 'then' of undefined', '; Zone:', 'ProxyZone', '; Task:', 'jasmine.onComplete', '; Value:', TypeError: Cannot read property 'then' of undefined
TypeError: Cannot read property 'then' of undefined
我尝试用导入中的
RouterTestingModule
替换提供者中的
Router
,但仍然出现相同的错误

有什么想法吗

谢谢大家!

===

编辑:添加了
routerStub

编辑2:分辨率

多亏@Inge Olaisen的输入,我才能够解决这个错误。答复:

1-带有
useClass
(w/promise)而不是
useValue

嗯,您没有提供任何关于routerstub的信息,或者它是如何设置的,但是您需要做的是将返回值模拟为承诺。因为它期望得到一个,但什么也得不到,所以它是未定义的

换句话说,navigate不会返回this.router

你可以试试:

class MockRoute {
navigate(routePath: any[], params: any) {
return new Promise();
}}
完整免责声明,我不习惯创建承诺,我习惯于可观察的方式,因此您可能需要正确地模拟新的承诺部分

不过,我建议您使用TypeMoq。那么您只需使用: const router=TypeMock.Mock.ofType(); setup(r=>r.navigate).返回(()=>newpromise())

然后使用router.object而不是routerStub,但它将是: {提供:路由器,使用工厂:()=>Router.object} 而不是 {提供:路由器,使用值:routerStub}