从Karma/Jasmine测试加载外部文件

Ale*_*exB 25 javascript json intellij-idea jasmine karma-runner

我正在尝试完成Jasmine测试(使用Karma和IntelliJ 13)来验证JSON文件.理想情况下,我的测试只是将JSON文件加载到数据对象中,然后让我解析以检查有效的格式和数据.我不需要在之前或之后验证函数,也不需要针对服务器进行测试.

我的基本设置是这样的:

it("should load an external file", function(){
var asyncCallComplete, result,
            _this = this;
        // asyncCallComplete is set to true when the ajax call is complete
        asyncCallComplete = false;

        // result stores the result of the successful ajax call
        result = null;

        // SECTION 1 - call asynchronous function
        runs(function() {
            return $.ajax('/test/config.json', {
                type: 'GET',
                success: function(data) {
                    asyncCallComplete = true;
                    result = data;
                },
                error: function() {
                    asyncCallComplete = true;
                }
            });
        });

        // SECTION 2 - wait for the asynchronous call to complete
        waitsFor(function() {
            return asyncCallComplete !== false;
        }, "async to complete");

        // SECTION 3 - perform tests
        return runs(function() {
            return expect(result).not.toBeNull();
        });
}
Run Code Online (Sandbox Code Playgroud)

问题是,无论我使用什么路径,我都会收到404错误,文件将无法加载.我尝试使用此测试服务从远程服务器加载外部JSON结果:

http://date.jsontest.com/

这很有效.

我的测试文件名为/test/mySpec.js,我的karma.conf.js文件位于根目录下.我已经将JSON文件移动到所有这些位置而没有运气.我究竟做错了什么?

更新与答案:

根据下面的答案,我将此添加到我的karma.conf.js:

// fixtures
{ pattern: 'test/*.json',
    watched: true,
    served:  true,
    included: false
}
Run Code Online (Sandbox Code Playgroud)

然后,我这样写了我的测试:

    var json:any;
    it("should load a fixture", function () {
        jasmine.getFixtures().fixturesPath = "base/test/"
        var f = readFixtures("registration.json");
        json = JSON.parse(f);
        expect(json).toBeDefined();

    })

    it("should have a title", function () {
        expect(json.title).toNotBe(null);
    })
    etc...
Run Code Online (Sandbox Code Playgroud)

它过去了.

jsp*_*ine 20

您是否通过karma.config.js提供JSON文件?

您可以通过fixture提供JSON文件:

files: [
      // angular 
      'angular.min.js',
      'angular-route.js',
      'angular-mocks.js',

      // jasmine jquery helper
     'jquery-1.10.2.min.js',
     'jasmine-jquery.js',

      //  app
      '../../public/js/app.js',

      // tests
      '*-spec.js',

      // JSON fixture
      { pattern:  '/test/*.json',
        watched:  true,
        served:   true,
        included: false }
    ],
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,jsplaine.我一直在尝试这个.出于某种原因,今天它起作用,虽然我不确定除了睡觉之外我做了什么不同的事情.:-) (2认同)
  • 它也适用于非json文件.说一个文本文件或.xlsx文件? (2认同)

Piz*_*her 8

通过夹具提供JSON是最简单的,但由于我们的设置,我们不能轻易做到这一点,所以我写了一个替代的辅助函数:

安装

bower install karma-read-json

用法

  1. 将karma-read-json.js放入您的Karma文件中,例如:

    files = [
    ...
    'bower_components/karma-read-json/karma-read-json.js',
    ...
    ]
    
    Run Code Online (Sandbox Code Playgroud)
  2. 确保您的JSON由Karma提供,例如:

    files = [
    ...
    {pattern: 'json/**/*.json', included: false},
    ...
    ]
    
    Run Code Online (Sandbox Code Playgroud)
  3. readJSON在测试中使用该功能.例:

    var valid_respond = readJSON('json/foobar.json');
    $httpBackend.whenGET(/.*/).respond(valid_respond);
    
    Run Code Online (Sandbox Code Playgroud)

  • 我已经安装了你的软件包并尝试没有成功,它给了我一个404,找不到文件错误(.json文件).你是如何使它工作的? (3认同)