【问题标题】:Converting a JSON object into a response object将 JSON 对象转换为响应对象
【发布时间】:2017-10-17 16:21:09
【问题描述】:

我正在尝试将模拟 JSON 数据传递到我的 jasmine 单元测试中。

我的 JSON 格式如下:

{   
   "CompanyResponse":{    
      "CompanyCreatedDate":"1990-10-02",    
      "RunDate":"2",   
      "LastChangedDate":"2015-10-02",   
      "CompanySummary": {   
         "Id":"Apple",   
         "MaximumCredit":"10000000",   
         "Margin":"60000000",    
         "Limit":"-1500000",    
         "HistoricData":{    
            "CompanyHistoricData": [   
               {  
                  "LaunchDate":"2008-08-31",  
                  "Product":"Iphone2",  
                  "TotalProductsCreated":"1000000",  
                  "TotalProductsSold":"800000",  
                  "TotalReturns":"200000",  
                  "TotalMargin":"600000"  
               },  
               {  
                  "LaunchDate":"2010-08-31",  
                  "Product":"Iphone4",  
                  "TotalProductsCreated":"2000000",  
                  "TotalProductsSold":"1500000",  
                  "TotalReturns":"350000",  
                  "TotalMargin":"800000"  
               }  
            ]  
         },  
         "RefurbishedData": {  
            "CompanyRefurbished": [  
               {  
                  "Id":"Apple.201221.12",  
                  "ProductId":"iph-213454",  
                  "StartDate":"2015-09-07",  
                  "FinishDate":"2015-09-10",  
                  "CostOfRefurbishing":"50"  
               },  
               {  
                  "Id":"Apple.201228.12",  
                  "ProductId":"iph-4155655",  
                  "StartDate":"2015-09-10",    
                  "FinishDate":"2015-09-12",  
                  "CostOfRefurbishing":"85"  
               }  
            ]  
         }  
      }  
   }  
}  

我正在使用上面的 JSON 传递给类似于下面的函数进行单元测试:

public getTotal(response: CompanyResponse): void {    
  var companySummary = response.CompanySummary;    
  
  //gets total 'CostOfRefurbishing' for all phones    
  var totalRefurbishmentAmount :number = 0;

  for (let companyRefurbishments of companySummary.RefurbishedData) {
    totalRefurbishmentAmount += Number.parseInt(companyRefurbishments.CostOfRefurbishing.toString());
  }
}

我面临的问题是我无法将CompanyResponse 作为一个整体传递给getTotal() 函数。即使我使用JSON.stringify(),它也不起作用,因为它只是将其转换为字符串,如果我使用JSON.parse(),它也不起作用,因为它将它转换回对象格式。

下面是我们在正常情况下如何调用getTotal() 方法:

export class MyService{    
 async CompanySummary(): Promise<CompanySummaryResponse>    
{    
    const response = await this.http.fetch('CompanySummary');    
    return await response.json();    
  }    
}    

var myService = new MyService();    
CompanySummary: CompanySummaryResponse;    
CompanySummary = await myService.CompanySummary();    
this.calculator.getTotal(CompanySummary);    

干杯, 大师

【问题讨论】:

  • Reallocations 在您的数据中的什么位置?
  • 您需要CompanyResponse 有一个构造函数,该构造函数将该对象或从该对象派生的东西作为参数
  • @torazaburo 对不起我的错。它在 JSON 中的 RefurbishedData,而不是重新分配
  • @arboreal84 ,这是单元测试的一部分,我试图不重组我的程序以使单元测试工作。在实际场景中,它是一个 HTTP 调用,它给了我真正的响应 JSON。

标签: javascript json jasmine aurelia


【解决方案1】:

您可以使用standard Response Interface from the JavaScript Fetch API 模拟响应对象吗?

如果您查看documentation for the constructor method - 它接受body 参数和init 选项对象。 body 参数可以是 Blob,所以你可以;

var data = {foo: "bar"};
var blob = new Blob([JSON.stringify(data, null, 2)], {type : 'application/json'});

var init = { "status" : 200 , "statusText" : "SuperSmashingGreat!" };
var myResponse = new Response(blob, init);

这将创建一个 Response 对象,您应该能够将其传递给您的测试。

【讨论】:

  • 上面的方法试过了,问题依旧。 getTotal 函数抱怨无法接受“响应”类型的参数
  • @GuruduttShenoy 当你正常使用getTotal 时 - 传递给它的是什么?
  • 已经添加了对 getTotal() 的正常调用以及服务调用,它通过它在上面发布的问题的最后一部分中获取 JSON 数据。 stackoverflow.com/users/297243/thebluefox
  • 。现在问题已经解决了。问题实际上在于将父元素“CompanyResponse”作为 JSON 文件的一部分传递。使用您之前提到的 FetchApi 的标准响应接口返回预期的响应。非常感谢您的帮助:) stackoverflow.com/users/297243/thebluefox
  • 我很高兴@GuruduttShenoy - 很高兴你把它整理好了!
猜你喜欢
  • 2018-07-26
  • 1970-01-01
  • 1970-01-01
  • 2019-03-15
  • 1970-01-01
  • 2021-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多