【问题标题】:How to access json files from karma tests如何从业力测试访问 json 文件
【发布时间】:2016-02-29 12:15:49
【问题描述】:

我正在尝试为我的 typescript 类创建一个测试,该类从 json 文件中读取数据..

readData<T>(filePath: string): Promise<T> {
    return Qajax.getJSON(filePath);
}

我正在使用 Jasmine 和 karma 环境进行测试。我的问题是,即使我在 karma 中添加任何 json 文件,它也不会加载到 chrome 启动器中

    files: [
        'libs/angular/angular.js',
        'libs/angular/angular-route.js',
        'libs/angular/angular-mocks.js',            
        'libs/collections/collections.js',
        'tests/framework/common/app_config.json',
        'tests/package.json',

        { pattern: 'libs/qajax/qajax.js', included: false },
        { pattern: 'libs/q/q.js', included: false },

        { pattern: 'tests/framework/**/*.js', included: false },
        { pattern: 'tests/toolpattern/**/*.js', included: false },
        { pattern: 'tests/test-main.js', included: true },

    ]

正如您在上面的业力配置中看到的,我添加了一个名为 app_config.json 的文件。现在我正在尝试从测试中读取该文件...

it("read data test.", function (done) {

    var promise = configurationAccessorImpl.readData("tests/framework/common/app_config.json");
    promise.then(result => {
        console.info("Get configuration test - Success.");
    }, error => {
        console.log(error);
    });

});

测试总是失败,因为 json 文件不存在..

【问题讨论】:

标签: json typescript jasmine karma-runner


【解决方案1】:

库 jasmine-jquery 为 Jasmine JavaScript 测试框架提供了几个扩展。 使用扩展 API 来处理测试规范中的 HTML、CSS 和 JSON 固定装置,以读取 .json 文件。

步骤: - 添加 jasmine-jquery.js 到 libs 和 jasmine-jquery.d.ts 到 /libs/typings ,在 karma.config 中将所需的引用添加到库中,添加 JSON 夹具位置以从以下位置读取文件:

   // list of files / patterns to load in the browser
    files: [
   …………………………..
        'libs/jquery/jquery-1.9.1.js',
        'libs/jasmine-jquery/jasmine-jquery.js',
   ………………….
          // JSON fixture
        {
            pattern: 'configuration/*.json',
            watched: true,
            served: true,
            included: false
        }
    ],

- 现在在业力测试中,可以读取文件如下:

 /// <reference path="../../../libs/typings/jasmine-jquery/jasmine-jquery.d.ts" />  
  ……………………    
/**
 *  @description Unit test for - Getting the configuration file in karma.     
 */
it("Configuration file accessor: Read configuration file test.", function (done) {

    jasmine.getJSONFixtures().fixturesPath = 'base/configuration/';

    var jsonData = getJSONFixture('app_config.json');
    var appConfig = JSON.stringify(jsonData);

   // Prints the config file content
    console.info(appConfig);
    expect(appConfig).toBeDefined();
});

【讨论】:

    【解决方案2】:

    不清楚函数configurationAccessorImpl.readData的作用。但是,如果您尝试使用 HTTP 请求加载 JSON 文件,您可能会遇到问题,因为 karma 从 /base 相对路径提供测试文件。

    尝试将您的测试更改为:

    var promise = configurationAccessorImpl.readData("/base/tests/framework/common/app_config.json");
    

    它可能会完成这项工作。

    【讨论】:

    • 这里的主要问题是 json 文件没有在 karma 环境中加载。当您从 Chrome 启动器检查源文件时,您看不到文件“/base/tests/framework/common/app_config.json”..所有其他 .js 文件都可见..
    猜你喜欢
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    相关资源
    最近更新 更多