gpt4 book ai didi

Angular 4 - 失败 : Can't resolve all parameters for ActivatedRoute: (?,?,?,?,?,?,?,?)

转载 作者:太空狗 更新时间:2023-10-29 16:53:07 25 4
gpt4 key购买 nike

我已经引用了以下链接来获得答案,但我找不到适合我的场景的任何有效解决方案。 Error: (SystemJS) Can't resolve all parameters for ActivatedRoute: (?, ?, ?, ?, ?, ?, ?, ?)

因此,我一直在尝试从提供者那里移除 Activated Route,但测试平台仍然没有通过。它显示

Error: No provider for ActivatedRoute!

这是我的代码,我想在使用 Jasmine 的 Angular 应用程序中运行我的测试平台。

import { ActivatedRoute } from '@angular/router';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterModule, Routes } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';

describe('SomeComponent', () => {
let component: SomeComponent;
let fixture: ComponentFixture<SomeComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ RouterModule, RouterTestingModule ],
declarations: [ SomeComponent ],
providers: [ ActivatedRoute ],
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(SomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});

获取错误

enter image description here

最佳答案

您想向您的组件注入(inject)一个伪造的 ActivatedRoute,因为您在测试中自己创建了它,因此路由器不会为您创建它并注入(inject)一个 ActivatedRoute。所以你可以使用这样的东西:

describe('SomeComponent', () => {

const fakeActivatedRoute = {
snapshot: { data: { ... } }
} as ActivatedRoute;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ RouterTestingModule ],
declarations: [ SomeComponent ],
providers: [ {provide: ActivatedRoute, useValue: fakeActivatedRoute} ],
})
.compileComponents();
}));
});

关于Angular 4 - 失败 : Can't resolve all parameters for ActivatedRoute: (?,?,?,?,?,?,?,?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48077725/

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