Lak*_*ara 21 router unit-testing testbed karma-jasmine angular
我已经提到了以下链接来获得答案,但我找不到任何适用于我的方案的解决方案. 错误:(SystemJS)无法解析ActivatedRoute的所有参数:(?,?,?,?,?,?,?,?)
因此,我一直试图从供应商中删除激活路线,但测试床仍未通过.表明
错误:没有ActivatedRoute的提供商!
所以这是我的代码,我想在使用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();
});
});
Run Code Online (Sandbox Code Playgroud)
得到错误
JB *_*zet 37
您希望向组件注入假的ActivatedRoute,因为您在测试中自己创建它,因此路由器不会为您创建它并注入ActivatedRoute.所以你可以使用这样的东西:
describe('SomeComponent', () => {
const fakeActivatedRoute = {
snapshot: { data: { ... } }
} as ActivatedRoute;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ RouterTestingModule ],
declarations: [ SomeComponent ],
providers: [ {provide: ActivatedRoute, useValue: fakeActivatedRoute} ],
})
.compileComponents();
}));
});
Run Code Online (Sandbox Code Playgroud)
Pet*_*ete 10
这是角度 7 的解决方案
{
provide: ActivatedRoute,
useValue: {
snapshot: {
paramMap: {
get(): string {
return '123';
},
},
},
},
},
Run Code Online (Sandbox Code Playgroud)
{
provide: ActivatedRoute,
useValue: {
snapshot: {
queryParamMap: {
get(): number {
return 6;
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15788 次 |
最近记录: |