gpt4 book ai didi

javascript - Angular-Jasmine,加载 json

转载 作者:行者123 更新时间:2023-12-03 08:25:46 27 4
gpt4 key购买 nike

我正在尝试从本地文件夹加载 json,然后将其输入到函数中进行测试。这是我正在尝试做的事情:

it('should be able to use treatExpResultJSON() function to treat incoming JSON', function() {
require(['./jsonSampleData.json'], function(json){
expect(dirService.treatExpResultJSON(json)).not.toBeNull();
expect(dirService.treatExpResultJSON(json)).not.toBeUndefined();
console.log(json);
console.log("Testing json passed!");
});

});

expectrequire 回调函数中不起作用,我猜,它根本没有被调用,因为 .not.toBeNull()not.toBeUndefine().toBeNull()toBeUndefine() 测试显示成功。此外,console.log() 不在函数内部工作。那么,我应该如何实际加载 json,然后将其提供给函数?

最佳答案

试试这个:

json = require('./jsonSampleData.json');

it('should be able to use treatExpResultJSON() function to treat incoming JSON', function() {
expect(dirService.treatExpResultJSON(json)).not.toBeNull();
expect(dirService.treatExpResultJSON(json)).not.toBeUndefined();
console.log(json);
console.log("Testing json passed!");
});

我在测试中使用了这个结构,并且有效。

关于javascript - Angular-Jasmine,加载 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33559483/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com