如何测试分配给函数 Jasmine 中的变量的方法

问题描述 投票:0回答:1

我正在使用 Angular 13。我正在尝试测试分配给变量的方法,如下所示:

constructor(route: ActivatedRoute) {}

compFunction(id: number) {
  if (id) {
    let result = this.returnFunction();
  }
}

returnFunction() {
  return this.route.snapshot.paramMap.get('id')
}

首先我试过这个:

it("should open foo page", inject((omitted)) => {
  component.compFunction('id')
  const data = component.returnFunction(); // not working this line
  data.toLowerCase();
})); 

但是我收到以下错误

无法读取未定义的属性(读取:'toLowerCase')

其次,我尝试模拟该函数并分配它,但它没有用。

angular jasmine
1个回答
0
投票

在进行测试时,您的路线中可能没有

id
参数,因此您的
returnFunction
函数返回未定义。你会想要模拟
route
给它一个
snapshot.paramMap
并在你的测试床上提供它。

Angular Mock ActivatedRoute 使用快照和 ParamMap

© www.soinside.com 2019 - 2024. All rights reserved.