Angular 有没有办法用玩笑来测试角度路由器?
我试图测试当用户导航到页面时,侧边栏中的元素是否正确折叠。在我的玩笑测试中,我很难模仿角度路由器 我正在尝试测试的代码:Angular 有没有办法用玩笑来测试角度路由器?,angular,testing,jestjs,angular-router,Angular,Testing,Jestjs,Angular Router,我试图测试当用户导航到页面时,侧边栏中的元素是否正确折叠。在我的玩笑测试中,我很难模仿角度路由器 我正在尝试测试的代码: this.router.events.pipe( filter(event => event instanceof NavigationEnd) ).subscribe((event: NavigationEnd) => { this.current = event.url;
this.router.events.pipe(
filter(event => event instanceof NavigationEnd)
).subscribe((event: NavigationEnd) => {
this.current = event.url;
if (this.current.includes('page1')) {
this._expandMenu(this.page1Toggle);
if (this.current.includes('page2')) {
this._expandMenu(this.page2Toggle);
}
}
this._closeMenu();
});
正在尝试模拟路由器:
let eventSubject: Subject<Event> = new Subject<Event>();
export class MockRouter {
events: Observable<Event> = eventSubject;
}
我已经尝试过像这样提供路由器{provide:Router,useClass:MockRouter}
但在尝试实例化设备时,会不断出现错误:
let fixture: ComponentFixture<SideBarComponent>;
let comp: SideBarComponent;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
SideBarComponent,
],
imports: [
RouterTestingModule
],
providers: [
{ provide: Router, useClass: MockRouter}
]
}).compileComponents();
fixture = TestBed.createComponent(SideBarComponent); --Cannot read property 'root' of undefined
comp = fixture.componentInstance;
let夹具:组件夹具;
let comp:SideBarComponent;
在每个之前(()=>{
TestBed.configureTestingModule({
声明:[
侧边栏组件,
],
进口:[
路由测试模块
],
供应商:[
{提供:路由器,useClass:MockRouter}
]
}).compileComponents();
fixture=TestBed.createComponent(SideBarComponent);--无法读取未定义的
comp=夹具。组件状态;
找到了一种更好的方法来处理Angular的显示/折叠功能值得注意的是,我使用Angular 8和Jest 24.8来实现这一点
let fixture: ComponentFixture<SideBarComponent>;
let comp: SideBarComponent;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
SideBarComponent,
],
imports: [
RouterTestingModule
],
providers: [
{ provide: Router, useClass: MockRouter}
]
}).compileComponents();
fixture = TestBed.createComponent(SideBarComponent); --Cannot read property 'root' of undefined
comp = fixture.componentInstance;