【问题标题】:How to mock params in routing in angular (jasmine)如何模拟角度路由中的参数(茉莉花)
【发布时间】:2021-07-15 13:18:27
【问题描述】:

如何模拟参数

this.id = params['id'];

(路由器角度)在茉莉花测试中?

我的代码:

constructor(public activatedRoute: ActivatedRoute, private store: Store) { }
          
ngOnInit(): void {
  this.store.dispatch(new LoadCategoriesAction());
  this.store.dispatch(new LoadProductsByMainAction(false));
  this.sub = this.activatedRoute.params.subscribe(params => {
    this.id = params['id'];
    console.log("!!!!!!!!!!!!!");
    console.log(this.id);
    if (this.id) {
      this.store.dispatch(new LoadProductByIdAction(this.id));
    }
  })
}

【问题讨论】:

标签: angular jasmine


【解决方案1】:

您必须为 ActivatedRoute 创建一个模拟,如下所示;

providers: [
...,
{provide: ActivatedRoute, useValue: {params: of({id: '123'})}}
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-01
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 2015-08-11
    相关资源
    最近更新 更多