我有以下sample.json文件。在jasmine-data-provider上找到的示例已针对简单JSON数据进行了说明。如何将以下json数据的值传递给数据提供者。
[
{
"company_name": "Google",
"Headquartered_at": "Mountain view",
"Department": [
{
"name": "Engineering",
"number_of_subDepartment": "3",
"subDepartments": [
{
"subDepartment_name": "Developers",
"no_of_employees": "10"
},
{
"subDepartment_name": "SRE",
"no_of_employees": "10"
},
{
"subDepartment_name": "QA",
"no_of_employees": "10"
}
]
}, {
"name": "Human resources",
"number_of_subDepartment": "0"
}]
},
{
"company_name": "Microsoft",
"Headquartered_at": "Redmond",
"Department": [
{
"name": "Engineering",
"number_of_subDepartment": "2",
"subDepartments": [
{
"subDepartment_name": "Developers",
"no_of_employees": "10"
},
{
"subDepartment_name": "QA",
"no_of_employees": "10"
}
]
},
{
"name": "Human resources",
"number_of_subDepartment": "0"
}]
},
{
"company_name": "Facebook",
"Headquartered_at": "Menlo park",
"Department": [
{
"name": "Engineering",
"number_of_subDepartment": "2",
"subDepartments": [
{
"subDepartment_name": "Developers",
"no_of_employees": "10"
},
{
"subDepartment_name": "QA",
"no_of_employees": "10"
}
]
},
{
"name": "Human resources",
"number_of_subDepartment": "0"
}]
}
]
规格文件中的用法示例如下
describe('test addition with data provider - provider function', function () {
function plusProvider() {
return [
{a: 2, b: 3, expected: 5},
{a: '14', b: 15, expected: 29},
{a: 12, b: '13', expected: 25},
{a: '22', b: '13', expected: 35},
];
}
using(plusProvider, function (data) {
it('should calc with operator +', function () {
var result = calculator.calc(data.a, data.b, '+');
expect(result).toEqual(data.expected);
});
});
});
由于sample.json文件更加复杂,我如何读取数据并将其分配给数据提供程序功能。您能指出我正确的资源吗?
答案 0 :(得分:2)
应该可以使用标准的JSON表示法和jasmine-data-provider以所需的任何方式与传递的JSON进行交互。还是要在数组中的每个JSON块上调用它?
datafile.json
[
{
"company_name": "Google",
"Headquartered_at": "Mountain view",
"Department": [
{
"name": "Engineering",
"number_of_subDepartment": "3",
"subDepartments": [
{
"subDepartment_name": "Developers",
"no_of_employees": "10"
},
{
"subDepartment_name": "SRE",
"no_of_employees": "10"
},
{
"subDepartment_name": "QA",
"no_of_employees": "10"
}
]
},
{
"name": "Human resources",
"number_of_subDepartment": "0"
}
]
},
{
"company_name": "Microsoft",
"Headquartered_at": "Redmond",
"Department": [
{
"name": "Engineering",
"number_of_subDepartment": "2",
"subDepartments": [
{
"subDepartment_name": "Developers",
"no_of_employees": "10"
},
{
"subDepartment_name": "QA",
"no_of_employees": "10"
}
]
},
{
"name": "Human resources",
"number_of_subDepartment": "0"
}
]
},
{
"company_name": "Facebook",
"Headquartered_at": "Menlo park",
"Department": [
{
"name": "Engineering",
"number_of_subDepartment": "2",
"subDepartments": [
{
"subDepartment_name": "Developers",
"no_of_employees": "10"
},
{
"subDepartment_name": "QA",
"no_of_employees": "10"
}
]
},
{
"name": "Human resources",
"number_of_subDepartment": "0"
}
]
}
]
代码
let using = require('jasmine-data-provider');
let file = require('./datafile.json');
using(file, (companyData) => {
console.log(companyData.company_name);
console.log(companyData.Headquartered_at);
for (let department of companyData.Department) {
console.log('Department name=' + department.name);
console.log('num sub departments=' + department.number_of_subDepartment);
if (department.number_of_subDepartment > 0) {
for (let subDepartment of department.subDepartments) {
console.log(subDepartment);
}
}
}
console.log();
//alternatively you can access in the standard way
console.log(companyData.Department[0].name);
console.log();
})