rails-assets-angular + karma测试

时间:2014-11-27 04:10:45

标签: ruby-on-rails angularjs unit-testing coffeescript karma-runner

我刚刚开始使用rails-assets.org来提供我的javascript文件,这些文件效果很好,但现在我的karma单元测试不起作用。

这是我的karma配置文件,问题是我删除了所有这些angular-1.2.22目录并通过gem访问它。

// Karma configuration
// Generated on Thu Aug 15 2013 13:47:52 GMT+1000 (EST)

module.exports = function(config) {
  config.set({

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

    // frameworks to use
    frameworks: ['jasmine'],

    // list of files / patterns to load in the browser
    files: [
        // libraries
        'app/assets/javascripts/keymaster/keymaster.js',

    'vendor/assets/javascripts/jquery.js',
        'vendor/assets/javascripts/angular-1.2.22/angular.min.js',
        'vendor/assets/javascripts/angular-1.2.22/angular-mocks.js',
        'vendor/assets/javascripts/angular-1.2.22/angular-resource.js',
        'vendor/assets/javascripts/ng-table-master/ng-table.min.js',
        'vendor/assets/javascripts/ui-utils/modules/keypress/keypress.js',
        'vendor/assets/javascripts/ui-utils/modules/mask/mask.js',
        'vendor/assets/javascripts/angularjs-country-select-master/angular.country-select.js',
        'vendor/assets/javascripts/*.js',

        // application code
        'app/assets/javascripts/*.js',
        'app/assets/javascripts/app/main.js.coffee',
        'app/assets/javascripts/config/*.js.coffee',
        'app/assets/javascripts/app/services/*.js.coffee',
        'app/assets/javascripts/app/resources/*.js.coffee',
        'app/assets/javascripts/app/controllers/*.js.coffee',
        'app/assets/javascripts/app/directives/*.js.coffee',

        // tests
        'spec/javascripts/unit/spec.js.coffee',
        'spec/javascripts/unit/*_spec.js.coffee'
    ],

    // test results reporter to use
    // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
    reporters: ['progress'],

    // web server port
    port: 9876,

    // enable / disable colors in the output (reporters and logs)
    colors: true,

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

    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,

    // If browser does not capture in given timeout [ms], kill it
    captureTimeout: 60000,

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

这就是我现在安装AngularJS javascript文件的方式

gem 'rails-assets-angular', '1.2.2' # Upgrading will change the order for ng-table for some reason.
gem 'rails-assets-angular-sanitize', '1.2.2'
gem 'rails-assets-angular-resource', '1.2.2'
gem 'rails-assets-angular-mocks', '1.2.2'
gem 'rails-assets-angular-ui-router', '~> 0.0.1'
gem 'rails-assets-map7--angularjs-country-select', '~> 0.0.1'
#gem 'rails-assets-angular-flash' # May not have my additions, need to check
gem 'rails-assets-ng-table', '~> 0.3.0'
gem 'rails-assets-lodash'

gem 'rails-assets-textAngular', '~> 1.2.2'
gem 'rails-assets-font-awesome'

这不会放一个我可以直接链接到的javascript文件。当角度通过导轨本身加载时,如何配置karma来加载角度?

更新

我发现本教程应修复此问题,但无法检测coffeescript并抱怨每个文件。

http://sebastien.saunier.me/blog/2014/02/04/angular--rails-with-no-fuss.html

现在,当我运行测试时,我收到此错误

INFO [karma]: Karma v0.12.28 server started at http://localhost:9876/
INFO [Chrome 37.0.2062 (Linux)]: Connected on socket EuxRBXdMyCfQDtjzOqaH with id manual-1854
Chrome 37.0.2062 (Linux) ERROR
  Uncaught SyntaxError: Unexpected token >
  at /home/map7/pais/app/assets/javascripts/general.js.coffee:1

general.js.coffee文件包含

$ ->

  $("#from, #to").datepicker
    dateFormat: "yy-mm-dd"

1 个答案:

答案 0 :(得分:1)

必须将以下内容添加到我的spec / karma / config / unit.js文件中

  preprocessors: {
      '../**/*.coffee': ['coffee']
  },

  coffeePreprocessor: {
      // options passed to the coffee compiler
      options: {
          bare: true,
          sourceMap: false
      },
      // transforming the filenames
      transformPath: function(path) {
          return path.replace(/\.coffee$/, '.js');
      }
  },

将以下包添加到我的package.json

  "devDependencies": {
    "karma-coffee-preprocessor": "^0.1.3"
  }

运行npm install

现在它在运行测试之前转换了coffeescript。