2015-07-12 95 views
2

這是我第一次測試一個應用程序,這有點讓人頭痛。我建立了測試環境。我爲我的測試文件夾中的茉莉index.html看起來是這樣的:茉莉花和Karma與骨幹的問題

的index.html

<!doctype html> 
<html> 
    <head> 
    <title>Jasmine Spec Runner</title> 
    <link rel="stylesheet" href="../bower_components/jasmine/lib/jasmine-core/jasmine.css"> 
    </head> 
    <body> 
    <script src="../bower_components/jasmine/lib/jasmine-core/jasmine.js"></script> 
    <script src="../bower_components/jasmine/lib/jasmine-core/jasmine-html.js"></script> 
    <script src="../bower_components/jasmine/lib/jasmine-core/boot.js"></script> 

    <!-- include source files here... --> 
    <script src="../public/js/main.js"></script> 
    <script src="../public/js/AppView.js"></script> 
    <script src="../public/js/FormLoanView.js"></script> 
    <script src="../public/js/FormLoanModel.js"></script> 
    <script src="../public/js/ResponseLoanModel.js"></script> 
    <script src="../public/js/ResultLoanView.js"></script> 

    <!-- include spec files here... --> 
    <script src="spec/test.js"></script> 
    </body> 
</html> 

test.js

(function() { 
describe('Form Model', function() { 

    describe('when instantiated', function() { 

    it('should exhibit attributes', function() { 
     var formModel = new FormLoanModel({}); 
     console.log(formModel) 
     expect(formModel.get("Annual Income")) 
     .toEqual(""); 
    }); 

    }); 

}); 
})(); 

打開當我index.html我得到以下信息:

TypeError: undefined is not a function 

所以它看起來像它運行我的測試。打開Chrome開發人員工具後,我得到以下內容:

Uncaught ReferenceError: Backbone is not defined 

因此,我意識到jQuery和Backbone未被加載到測試中。我來了解到,Karma幫助我們實現了很多自動化。使用Yeoman建立業力後。我做編輯我karma.conf.js現在看起來是這樣的:

// Karma configuration 
// http://karma-runner.github.io/0.12/config/configuration-file.html 
// Generated on 2015-07-12 using 
// generator-karma 1.0.0 

module.exports = function(config) { 
    'use strict'; 

    config.set({ 
    // enable/disable watching file and executing tests whenever any file changes 
    autoWatch: true, 

    // base path, that will be used to resolve files and exclude 
    basePath: '', 

    // testing framework to use (jasmine/mocha/qunit/...) 
    // as well as any additional frameworks (requirejs/chai/sinon/...) 
    frameworks: [ 
     "jasmine" 
    ], 

    // list of files/patterns to load in the browser 
    files: [ "../lib/*.js","../public/js/*.js","./spec/*.js" 
    ], 

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

    // web server port 
    port: 8080, 

    // Start these browsers, currently available: 
    // - Chrome 
    // - ChromeCanary 
    // - Firefox 
    // - Opera 
    // - Safari (only Mac) 
    // - PhantomJS 
    // - IE (only Windows) 
    browsers: [ 
     "Chrome" 
    ], 

    // Which plugins to enable 
    plugins: [ 
     "karma-phantomjs-launcher", 
     "karma-jasmine" 
    ], 

    // Continuous Integration mode 
    // if true, it capture browsers, run tests and exit 
    singleRun: false, 

    colors: true, 

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

    // Uncomment the following lines if you are using grunt's server to run the tests 
    // proxies: { 
    // '/': 'http://localhost:9000/' 
    // }, 
    // URL root prevent conflicts with the site root 
    // urlRoot: '_karma_' 
    }); 
}; 

我添加的文件是庫,我的骨幹模塊,和我的茉莉花測試。打字karma start後,我得到了以下成功的屏幕在終端指定的本地服務器:

Karma v0.12.37 - connected 
Chrome 43.0.2357 (Mac OS X 10.10.2) is idle 

所以最後在這一點上,我會刷新正常運行我的測試後,希望index.html,但事實並非如此。它仍然警告我缺乏骨幹和jQuery知識。任何人都可以幫我弄清楚我要去哪裏錯了嗎?

文件Strucutre

ROOT 
-----lib 
--------------backbone.js 
--------------underscore.js 
--------------jquery-1.11.3.js 
-----public 
--------------js 
---------------------*backbone modules* 
-----test 
--------------spec 
----------------------test.js 
--------------index.html 
--------------karma.conf.js 
+0

backbone.js位於何處?您是否嘗試明確地將文件路徑寫入karma.config文件的文件數組中?也許你的glob模式關閉了,它們不匹配必要的文件,所以它們不包括在內。 – doldt

+0

所以我正在按照正確的過程?這是路徑問題嗎? – theamateurdataanalyst

+0

我猜它**實際上是一個路徑問題。骨幹的js文件位於文件系統的哪裏? – doldt

回答

0

的「基本路徑」和「文件」你噶配置工作的屬性在一起就可以提供必要的文件,在瀏覽器的測試環境。

基本路徑將從Karma運行的工作目錄進行評估,因此如果您在項目的根目錄中運行它,那麼'../libs/*.js'將不會是匹配路徑爲您的bower_component JavaScript文件,最有可能的。

如果您的靜態文件文件夾樹未在項目的根目錄中啓動,請確保'basePath'指向您的靜態文件文件夾樹啓動的位置。

嘗試../bower_components/**/*.js../**/*.js(文件斑點圖案樣式)或嘗試添加指向每一個單獨的條目「文件」,即

'../bower_components/jquery/lib/jquery.min.js', '../bower_components/jquery/lib/backbone.min.js'

等,當然,使得這些條目指向真實的地點。我猜只是這個文件路徑問題阻止了它們被發現,因此Karma的測試服務器沒有向瀏覽器提供它們。