How to load external Json file using karma+Jasmine for angularJS testing?/

前端 未结 3 965
终归单人心
终归单人心 2020-12-17 22:38

Can anyone provide me an example in PLUNKER that how to load JSON file for karma/jasmine test.I want to read the data from JSON file for the test cases i am writing.I have b

3条回答
  •  离开以前
    2020-12-17 23:29

    Do you want to read the JSON file from a webserver or a local file system? No one can give an example of loading from a local file system from Plunker, since it runs in a web browser and is denied access to the file system.

    Here is an example of how to load a JSON file from disk in any Node.js program, this should work for Karma/Jasmine:

    var fs = require('fs');
    var filename = './test.json';
    
    fs.readFile(filename, 'utf8', function (err, data) {
        if (err) {
            console.log('Error: ' + err);
            return;
        }
    
        data = JSON.parse(data);
    
        console.dir(data);
    });
    

提交回复
热议问题