2016-07-23 49 views
0

我試圖使用業力運行單元測試,並且出現錯誤You need to include some adapter that implements __karma__.start method!。我試着用gruntkarma start命令運行。我做了谷歌搜索,所有的解決方案都沒有解決。不知道我做錯了什麼。我在karma.conf.js文件的插件下包含了帶有karma-jasmine的正確適配器,它有__karma__.start方法。這是我的配置文件: -錯誤:您需要包含一些實現__karma __的適配器。啓動方法

module.exports = function(config){ 
config.set({ 
// root path location that will be used to resolve all relative paths in files and exclude sections 
basePath : '../', 

files : [ 
    'bower_components/angular/angular.js', 
    'bower_components/angular-mocks/angular-mocks.js', 
    'node_modules/requirejs/require.js', 
    'node_modules/karma-jasmine/lib/adapter.js', 
    'app.js', 
    'mainapp/mainapp.js', 
    'mainapp/notes/notes.js', 
    'mainapp/notes/partial/create/create.js', 
    'mainapp/notes/partial/create/create-spec.js' 
], 

// files to exclude 
exclude: [ 
    'bower_components/angular/angular/*.min.js' 
], 

// karma has its own autoWatch feature but Grunt watch can also do this 
autoWatch : false, 

// testing framework, be sure to install the correct karma plugin 
frameworks: ['jasmine', 'browserify', 'requirejs'], 

// browsers to test against, be sure to install the correct browser launcher plugins 
browsers : ['PhantomJS'], 

// map of preprocessors that is used mostly for plugins 
preprocessors: { 
    'mainapp/notes/partial/create/create-spec.js' : ['browserify'] 
}, 

reporters: ['progress'], 

// list of karma plugins 
plugins : [ 
    'karma-teamcity-reporter', 
    'karma-chrome-launcher', 
    'karma-phantomjs-launcher', 
    'karma-babel-preprocessor', 
    'karma-requirejs', 
    'karma-jasmine', 
    'karma-browserify' 
], 

singleRun: true 

})} 

回答

0

使用requirejs框架關閉的__karma__.start自動調用。您需要創建一個文件a)配置RequireJS和b)調用__karma__.start來啓動測試。這是一個例子。它掃描Karma正在服務的文件以查找包含測試的文件。這是基於命名約定,任何以spec.jstest.js結尾的文件都是測試文件。它將文件名稱轉換爲模塊名稱。然後它配置RequireJS。它所做的一件事就是將所有測試模塊作爲deps,以便所有測試模塊立即加載。它將__karma__.start設置爲callback,這樣當加載所有通過deps的模塊時,測試開始。

var allTestFiles = []; 
var TEST_REGEXP = /(spec|test)\.js$/i; 

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

require.config({ 

    baseUrl: '/base', 

    paths: { 
     'chai':    'node_modules/chai/chai', 
     'chai-jquery':  'node_modules/chai-jquery/chai-jquery', 
     'jquery':   'node_modules/jquery/dist/jquery.min', 
     'underscore':  '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min', 
     'sn/sn-underscore': 'static/scripts/sn/sn-underscore', 
     'vendor/jquery-ui': '//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min' 
    }, 

    deps: allTestFiles, 

    callback: window.__karma__.start 
}); 

您需要包括這個文件在你karma.conf.js文件的files參數。由於您使用的是requirejs框架,因此您只需將它放在列表中。例如,如果你調用文件test-main.js(如噶文檔中的建議):

files: [ 
    'test-main.js', 
    ... 
] 

如果您在files上市了自己加載RequireJS,你需要把test-main.js RequireJS後。

+0

爲什麼有人應該處理這個文件...? – Jim

+0

是的,我忘了提及如何包含它。我編輯了答案補充說。 – Louis

相關問題