在Karma-Jasmine测试中获取外部文本文件的内容

时间:2015-06-21 02:17:09

标签: javascript jasmine karma-runner karma-jasmine gulp-karma

我一整天都在这里,并且几乎没有取得任何进展。我有一个Javascript解释器,它接受文本代码,我试图使用Karma-Jasmine测试来测试它。因为我不想在每个规范中嵌入代码,所以我尝试从外部文本文件加载这些项目。

到目前为止,我已经将文本文件设置为karma.config.json中的一个夹具,但我似乎无法弄清楚如何将其实际读入Javascript字符串。

这是一个示例规范:

jasmine.getFixtures().fixturesPath = 'base/codeFiles';
var testCode = loadFixtures("base/codeFiles/correct_1.txt");
console.log(f);

describe("#interpreter_load_test", function() {
  it("loads the code correctly", function() {
    var INTERPRETER = getInterpreter();

    INTERPRETER.initialize();
    INTERPRETER.loadCode(testCode);
    var codeObj = INTERPRETER.code;
    var numLines = codeObj.codeLength;
    var loadedCode = codeObj.codeString;
    expect(numLines).toBe(16);
    expect(loadedCode).toBe(testCode);
  });
});

这是我的karma.config.json

// Karma configuration
// Generated on Sat Jun 20 2015 21:46:22 GMT+0000 (UTC)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine-jquery', 'jasmine'],


    // list of files / patterns to load in the browser
    files: [
      'js/build/production.min.js',
      'js/tests/*.js',
      {
        pattern: 'codeFiles/*.txt',
        watched: true,
        served: true,
        included: false,
      }
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['PhantomJS', 'Chrome', 'Firefox', 'IE', 'Safari'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    proxies: {
      '/codeFiles': 'http://localhost:9876/base/codeFiles',
    },
  });
};

此时我的头撞在墙上,所以任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

最后我决定切换到Mocha并使用fs包,我可能应该首先这样做。