2015-07-12 68 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的测试服务器没有向浏览器提供它们。