2016-06-22 32 views
0
加载JSON文件

我的文件夹结构是这样的:无法使用卡玛 - 读JSON插件

src 
    -cars 
     car.controller.js 
     car.controller.spec.js 
     car.test-data.json 
在我的规格文件

我读了JSON文件,如下所示:

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

不过我继续收到错误..找不到文件。

我已经尝试了很多不同的路径..都不似乎工作

回答

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 
    .... 
}