Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 茉莉花的烦恼和骨气的业力_Javascript_Jquery_Backbone.js_Karma Jasmine - Fatal编程技术网

Javascript 茉莉花的烦恼和骨气的业力

Javascript 茉莉花的烦恼和骨气的业力,javascript,jquery,backbone.js,karma-jasmine,Javascript,Jquery,Backbone.js,Karma Jasmine,这是我第一次测试一个应用程序,有点头疼。我已经建立了一个测试环境。我的测试文件夹中jasmine的index.html如下所示: // 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)

这是我第一次测试一个应用程序,有点头疼。我已经建立了一个测试环境。我的测试文件夹中jasmine的
index.html
如下所示:

// 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_'
  });
};
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>
打开我的
index.html
时,我收到以下消息:

TypeError: undefined is not a function
看来它正在运行我的测试。打开chrome developer tools后,我得到以下信息:

Uncaught ReferenceError: Backbone is not defined
所以我意识到jQuery和主干并没有被加载到测试中。我开始了解到,因果报应帮助我们将很多事情自动化。在使用约曼建立业力之后。我对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_'
  });
};
我添加的文件是库、主干模块和Jasmine测试。键入
karma start
后,我在终端指示的本地服务器上获得以下成功屏幕:

Karma v0.12.37 - connected
Chrome 43.0.2357 (Mac OS X 10.10.2) is idle
最后,我希望刷新后的
index.html
能够正确运行我的测试,但事实并非如此。它仍然在警告我它缺乏主干网和jQuery的知识。有人能帮我找出哪里出了问题吗

文件结构

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

Karma配置的“basePath”和“files”属性协同工作,在浏览器中为测试环境提供必要的文件

基本路径将从运行Karma的工作目录进行计算,因此如果在项目的根目录中运行,则“../libs/*.js”很可能不是bower_组件javascript文件的匹配路径

如果静态文件夹树未在项目的根目录中启动,请确保“basePath”指向静态文件夹树的启动位置

尝试
。/bower\u components/***.js
。/***.js
(文件blob模式样式),或尝试向指向每个文件的“文件”添加单个条目,即

“../bower_components/jquery/lib/jquery.min.js”,
“../bower_components/jquery/lib/backbone.min.js”


等等,当然,让这些条目指向真实的位置。我猜正是这个文件路径问题阻止了它们被找到,因此Karma的测试服务器没有将它们提供给浏览器。

backbone.js的确切位置在哪里?您是否尝试将文件路径显式写入karma.config文件的files数组?也许你的glob模式是关闭的,它们与必要的文件不匹配,所以它们不包括在内。所以我正确地遵循了这个过程?这不是路径的问题吗?我猜这实际上是路径的问题。主干的js文件在您的文件系统中位于哪里?@doldt我添加了我的项目目录。这似乎与您的文件路径相对应。。。当您在Chrome中打开开发者控制台(您正在其中运行测试!)时,您能在开发工具的Sources选项卡中找到backbone.js吗?