如何为处理多个API调用的功能编写测试用例?

时间:2019-03-18 10:05:40

标签: jasmine angular6 karma-jasmine

我正在使用angular 6在茉莉花和业力中学习测试用例。我想了解如何为处理多个API调用(嵌套)的函数编写测试用例。这意味着第一个API的响应将作为参数发送到第二个API调用...

以下是代码段

组件功能代码

getData() {
 this.service.getDetails(null)
 .toPromise()
 .then((data) => {
   // Some calculation on the data object
   this.service.getDetails(data.value)
   .toPromise()
   .then((response) => {
      // Again some calculation.
   });
 });
}

我尝试按照以下方式为上述代码编写测试用例,但对我没有用。

在规格文件中

// In before each 
const data = jasmine.createSpyObject('service', '[getDetails]');
mockData = data.getDetails.and.returnValue(of(mockData));

//Actual test case 
expect(mockData).toHaveBeenCalledWith(null);
expect(mockData).tohaveBeenCalledWith(data); // For second API call.

请给我建议解决方案。

上述解决方案出现以下错误:

Spy to have been called with [[]] but actual calls were []

0 个答案:

没有答案
相关问题