2015-11-06 64 views
0

我想從本地文件夾中加載json,然後將其饋入函數來測試它。這裏是我想要做的事:Angular-Jasmine,加載json

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是不require回調函數內的工作,我想,這是不是在所有的,因爲既.not.toBeNull()not.toBeUndefined().toBeNull()toBeUndefined()的測試顯示出成功稱爲。此外,console.log()不在該功能內部工作。那麼,我應該如何加載json,然後將其提供給函數?

回答

1

試試這個:

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

我在我的測試,以及作品使用這種結構。