【发布时间】:2016-10-25 07:48:36
【问题描述】:
我正在尝试测试一个订阅路由器事件的简单组件,但我不知道如何模拟这部分代码。错误如下:Cannot read property 'pairwise' of undefined。
无法读取未定义的“成对”属性
这是我的测试:
describe('AppComponent', function () {
let fixture: ComponentFixture<AppComponent>;
var tendersServiceStub = {};
var scrollServiceStub = {};
var routerStub = {};
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [AppComponent],
providers: [
{provide: TendersService, useValue: tendersServiceStub},
{provide: ScrollService, useValue: scrollServiceStub},
{provide: Router, useValue: routerStub}
],
imports: [RouterTestingModule],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents().then(() => {
fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges()
});
}));
it('should instantiate component', () => {
// console.log(fixture); UNDEFINED
});
});
这是组件:
export class AppComponent {
appTitle: string = 'Magic';
constructor(private router: Router, private tendersService: TendersService, private scrollService: ScrollService) {
this.router.events.pairwise().subscribe((e) => {
if ((e[0] instanceof NavigationEnd) && (e[1] instanceof NavigationStart)) {
this.resetSearchCache(e);
}
})
}
private resetSearchCache(e: any) {
if ((e[1].url == '/home') || (e[1].url == '/advancedSearch')) {
if (!e[0].url.includes('/detail')) {
//DO SOMETHING
}
}
}
}
有什么想法吗?
提前致谢。
【问题讨论】:
-
你“存根”的路由器是一个空对象,所以你没有属性事件和成对
标签: angular unit-testing selenium karma-runner