2015-04-23 71 views
6

我正在嘗試使用業力配置自動測試。 我的文件結構如下Karma和RequireJS獲取基址以外的文件

/assests 
/css 
/font 
/img 
/js 
    /collection 
    /lib 
    /model 
    /plugin 
    /spec 
    /view 
    test-main.js 
    main.js 
/templates 
index.html 
karma.conf.js 

karma.conf.js:

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', 'requirejs'], 

    // list of files/patterns to load in the browser 
    files: [ 
    'js/test-main.js', 
     {pattern: 'js/*.js', included: false}, 
     {pattern: 'js/collection/*.js', included: false}, 
     {pattern: 'js/lib/**/*.js', included: false}, 
     {pattern: 'js/lib/**/**/*.js', included: false}, 
     {pattern: 'js/lib/**/**/**/*.js', included: false}, 
     {pattern: 'js/model/*.js', included: false}, 
     {pattern: 'js/model/**/*.js', included: false}, 
     {pattern: 'js/plugin/*.js', included: false}, 
     {pattern: 'js/plugin/**/*.js', included: false}, 
     {pattern: 'js/spec/*.js', included: false}, 
     {pattern: 'js/spec/**/*.js', included: false}, 
     {pattern: 'js/view/**/*.js', included: false}, 
     {pattern: 'js/view/*.js', included: false} 
    ], 


    // list of files to exclude 
    exclude: [ 
     'js/main.js', 
     'js/initScript.js', 
     'js/SpecRunner.js' 
    ], 
    etc. . . 
} 

測試main.js:

var allTestFiles = []; 
var TEST_REGEXP = /spec/i; 

var pathToModule = function(path) { 
    return path.replace(/^\/base\//, '').replace(/\.js$/, ''); 
}; 

Object.keys(window.__karma__.files).forEach(function(file) { 
    if (TEST_REGEXP.test(file)) { 
    // Normalize paths to RequireJS module names. 
    allTestFiles.push(pathToModule(file)); 
    } 
}); 

require.config({ 
    // Karma serves files under /base, which is the basePath from your  config file 
    baseUrl: '../', 

    // dynamically load all test files 
    deps: allTestFiles, 

    // we have to kickoff jasmine, as it is asynchronous 
    callback: window.__karma__.start, 

    paths: { 
    'jquery' : 'lib/jqm/jquery-1.11.2.min', 
    'underscore' : 'lib/underscore/underscore', 
    'backbone' : 'lib/backbone/backbone',  
    'template' : '../template', 
}, 

    shim : { 
     backbone : { 
      deps : [ 'underscore', 'jquery' ], 
      exports : 'Backbone' 
     } 
    } 
}); 

加載所有的js文件很好地工作。目前的問題是我無法加載基本路徑之外的任何資源。

所以當我的測試試圖獲取一個模板(在'../template/'下),karma無法找到這些模板並失敗。

我無法更改我的基本路徑,因爲所有的JS模塊都在'js /'之下。

是否有加載基本路徑之外的資源?

回答

0

正如你寫的

// Karma serves files under /base, which is the basePath from your  config file 
baseUrl: '../', 

因此,這意味着,如果你希望能夠訪問比您需要將所有網址具有換擋更深的文件:

baseUrl: '../..', 

你可以也可以使用base/這將被業力理解爲你的karma的基路由Config.js

爲了更好地理解karma.conf和test-main.js的basePath如何工作,你可以看看這個:http://monicalent.com/blog/2015/02/11/karma-tests-angular-js-require-j/

相關問題