无法使用karma-read-json插件

时间:2016-06-22 17:52:38

标签: jasmine karma-runner karma-jasmine

我的文件夹结构如下:

 src
   -cars
      car.controller.js
      car.controller.spec.js
      car.test-data.json

在我的spec文件中我正在读取json文件,如下所示:

var sampleData = readJSON('./car.test-data.json')

但是我一直收到错误..找不到文件。

我尝试了许多不同的路径..似乎没有工作

1 个答案:

答案 0 :(得分:0)

我终于找到了我在博客上找到的替代解决方案:

mockedDashboardJSON.js:

'use strict'
angular.module('mockedDashboardJSON',[])
.value('defaultJSON',{
    fakeData1:{'really':'fake2'},
    fakeData2:{'history':'faked'}
});
Then in your test file:

beforeEach(module('yourApp','mockedDashboardJSON'));
var YourControlNameCtrl, scope, $httpBackend, mockedDashboardJSON;
beforeEach(function(_$httpBackend_,defaultJSON){
    $httpBackend.when('GET','yourAPI/call/here').respond(defaultJSON.fakeData1);
    //Your controller setup 
    ....
});

it('should test my fake stuff',function(){
    $httpBackend.flush();
    //your test expectation stuff here
    ....
}
相关问题