I am trying to pass mock JSON data into my jasmine unit test.
The format of my JSON is like the one below:
{
"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"
}
]
}
}
}
}
I am using the above JSON to pass on to a function similar to the one below for unit testing:
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());
}
}
The problem I am facing is that I am not able to pass CompanyResponse
as a whole to the getTotal()
function. It doesn't work even if I use JSON.stringify()
because it just converts it to a string and it doesn't work if I use JSON.parse()
either as it converts it back into the Object format.
Here's how we call the getTotal()
method in a normal scenario:
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);
Cheers, Guru
Reallocations
in your data? – HydrosphereCompanyResponse
to have a constructor that takes that object, or something derived from that object, as parameter – Pestana