角茉莉,装载JSON [英] Angular-Jasmine, loading json
问题描述
我想从本地文件夹加载JSON,然后将其送入功能进行测试。这里是我想要做的:
I am trying to load json from local folder and then feed it into function to test it. Here is what I am trying to do:
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!");
});
});
期望
不是内部组件的要求
的回调函数,我想,这是不是在所有因为具有所谓的无论 .not.toBeNull()
, not.toBeUndefined()
和 .toBeNull()
, toBeUndefined()
的测试显示出成功。此外,的console.log()
未在函数内部工作。所以,我应该怎么实际加载的JSON,然后将它提供给函数?
expect
is not working inside the require
callback function, I guess, it is not called at all since with both .not.toBeNull()
, not.toBeUndefined()
and .toBeNull()
, toBeUndefined()
the test is showing success. Also, console.log()
are not working inside the function. So, how should I actually load the json, then supply it to the function?
推荐答案
试试这个:
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!");
});
我用这个结构在我的测试中,和作品。
I use this structure in my tests, and works.
这篇关于角茉莉,装载JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!