我的ActivatedRoute
代码是map
,后跟subscribe
。
export class MyComponent implements OnInit {
constructor(private actr: ActivatedRoute) {
this.actr.data.map(data => data.aList).subscribe((res) => {
console.log('res ------------------------- ', res); // It is not showing this.
this.myResponse = res;
});
}
ngOnInit() {
// some code
// myResponse is undefined
}
}
我已经在规格文件中尝试过
class MockActivatedRouter {
public map = (data) => data.aList = of({some: [] });
// Or
public map = (data) => of({some: [] });
}
它们都不起作用。
答案 0 :(得分:1)
我认为您想像这样模拟它:
class MockActivatedRouter {
data = of({some: [] }); // Dont remember the old rxjs5 syntax but you might want to use Observable.of(...)
}
手段ActivatedRoute
具有属性data
,您可以在构造函数中对其进行观察。
我通常只用简单的对象来模拟它,并提供可观察的Subject
以便能够在测试中随时发出。
我认为您知道如何在测试中提供模拟内容:
TestBed.configureTestingModule({
...
providers: [{
provide: ActivatedRoute,
useClass: MockActivatedRouter
}]
});
答案 1 :(得分:0)
事实证明,我们可以简单地做到:
class MockActivatedRouter {
public map = (data) => of(MyResponse.data.aList);
}
class MyResponse {
static data = {
// data
}
}