Получить содержимое внешнего текстового файла в тесте Karma-Jasmine

Я занимался этим целый день и практически не продвинулся. У меня есть интерпретатор 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',
    },
  });
};

В этот момент я бьюсь головой о стену, поэтому любая помощь будет принята с благодарностью.


person Ginour    schedule 21.06.2015    source источник


Ответы (1)


В конце концов я решил переключиться на Mocha и использовать пакет fs, что мне, вероятно, следовало сделать в первую очередь.

person Ginour    schedule 15.07.2015