从嵌套 json 向茉莉数据提供者赋值?

Assigning value to jasmine-data-provider from nested json?

我有以下 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 文件更复杂,我该如何读取数据提供程序函数并为其赋值。你能给我指出正确的资源吗?

应该可以通过 jasmine-data-provider 使用标准 JSON 符号以您需要的任何方式与传递的 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();
})