角茉莉,装载JSON [英] Angular-Jasmine, loading json

查看:134
本文介绍了角茉莉,装载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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆