我整天都在做这件事,几乎没有取得任何进展。我有一个 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',
},
});
};
在这一点上,我正在把头撞在墙上,所以任何帮助将不胜感激。