Angular-Jasmine, loading json

早过忘川 提交于 2019-12-11 23:33:43

问题


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!");
   });   

});

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?


回答1:


Try this:

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.



来源:https://stackoverflow.com/questions/33559483/angular-jasmine-loading-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!