Angular 4-失败:无法解析ActivatedRoute的所有参数:(?,?,?,?,?,?,?,?) [英] Angular 4 - Failed: Can't resolve all parameters for ActivatedRoute: (?, ?, ?, ?, ?, ?, ?, ?)
问题描述
我已经通过下面的链接获得了答案,但是找不到适合我的方案的可行解决方案. 错误:(SystemJS)无法解析ActivatedRoute的所有参数:(? ,?,?,?,?,?,?,?)
I have referred the following link to get the answers, but I couldn't find any working solution for my scenario. Error: (SystemJS) Can't resolve all parameters for ActivatedRoute: (?, ?, ?, ?, ?, ?, ?, ?)
因此,我一直在尝试从提供者中删除激活的路由",但测试床仍未通过.它显示了
Therefore, I have been trying to remove the Activated Route from the providers and still the test bed is not passing. It shows
错误:没有提供ActivatedRoute的提供商!
Error: No provider for ActivatedRoute!
这是我的代码,我想在使用 Jasmine的angular应用程序中运行测试台.
So here is my code, I want to run my test bed in the angular application which is using Jasmine.
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();
});
});
获取错误
推荐答案
您想向组件注入假的ActivatedRoute,因为您是在测试中自行创建的,因此路由器不会为您创建并注入一个ActivatedRoute.因此,您可以使用以下内容:
You want to inject a fake ActivatedRoute to your component, since you create it yourself in the test, and the router thus doesn't create it for you and inject an ActivatedRoute. So you can use something like this:
describe('SomeComponent', () => {
const fakeActivatedRoute = {
snapshot: { data: { ... } }
} as ActivatedRoute;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ RouterTestingModule ],
declarations: [ SomeComponent ],
providers: [ {provide: ActivatedRoute, useValue: fakeActivatedRoute} ],
})
.compileComponents();
}));
});
这篇关于Angular 4-失败:无法解析ActivatedRoute的所有参数:(?,?,?,?,?,?,?,?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!