AngularJS Foundations

Rails 环境支持Karma

// RAILS_ROOT/package.json
{
  "name": "rails-angular-karma-example",
  "dependencies": {
    "karma": ">= 0.12.16",
    "karma-jasmine": ">= 0.2.2",
    "karma-coffee-preprocessor": ">= 0.2.1",
    "karma-slim-preprocessor": ">= 0.0.1",
    "karma-phantomjs-launcher": ">= 0.1.4",
    "karma-ng-html2js-preprocessor": "git://github.com/monterail/karma-ng-html2js-preprocessor#feature/strip-sufix"
  }
}
# RAILS_ROOT/Gemfile
source 'https://rails-assets.org'
gem 'coffee-rails'


gem 'rails-assets-angular', '~> 1.2.0'
gem 'rails-assets-angular-cache', '~> 2.3.7'

group :development, :test do
  gem 'spring'
  gem 'rails-assets-angular-mocks'
end
bundle install
npm install
mkdir -p spec/javascripts
mkdir -p spec/karma
# vim spec/karma/application_spec.js
//= require application
//= require angular-mocks
// vim spec/karma/config/unit.js
// Karma configuration
// Generated on Mon Feb 03 2014 16:16:15 GMT+0100 (CET)

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

    // base path, based on tmp/ folder
    basePath: '../',

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

    // list of files / patterns to load in the browser
    files: [
      APPLICATION_SPEC,
      'app/assets/templates/**/*.slim',
      'spec/javascripts/**/*_spec.{coffee,js}'
    ],

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

    // 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,

    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera (has to be installed with `npm install karma-opera-launcher`)
    // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
    // - PhantomJS
    // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
    browsers: ['PhantomJS'],

    // 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,

    // Preprocessors
    preprocessors: {
      '**/*.coffee': ['coffee'],
      '**/*.slim': ['slim', 'ng-html2js']
    },

    ngHtml2JsPreprocessor: {
      stripPrefix: 'app/assets/templates/',
      stripSufix: '.slim'
    }
  });
};
bundle exec spring binstub --all
// vim bin/rake

#!/usr/bin/env ruby
begin
  load File.expand_path("../spring", __FILE__)
rescue LoadError
end
require_relative '../config/boot'
require 'rake'
Rake.application.run
# vim lib/tasks/karma.rake

namespace :karma  do
  task :start => :environment do
    with_tmp_config :start
  end

  task :run => :environment do
    exit with_tmp_config :start, "--single-run"
  end

  private

  def with_tmp_config(command, args = nil)
    # Change to [.., '.coffee'] for any CS config files
    Tempfile.open(['karma_unit', '.js'], Rails.root.join('tmp')) do |f|
      f.write unit_js(application_spec_files)
      f.flush

      system "./node_modules/karma/bin/karma #{command} #{f.path} #{args}"
    end
  end

  def application_spec_files
    Rails.application.assets.find_asset("application_spec.js").to_a.map {|e| e.pathname.to_s }
  end

  def unit_js(files)
    unit_js = File.open('spec/karma/config/unit.js', 'r').read
    unit_js.gsub "APPLICATION_SPEC", "\"#{files.join("\",\n\"")}\""
  end
end
# vim  lib/templates_paths.rb
module TemplatesPaths
  extend self

  def templates
    Hash[
      Rails.application.assets.each_logical_path.
      select { |file| file.end_with?('swf', 'html', 'json') }.
      reject { |file| file.end_with?('/bower.json') }.
      reject { |file| file.end_with?('/composer.json') }.
      reject { |file| file.starts_with?('angular-ui-router') }.
      map { |file| [file, ActionController::Base.helpers.asset_path(file)] }
    ]
  end
end
// vim spec/karma/application_spec.js

//= require application
//= require angular-mocks
# vim config/environments/development.rb
config.assets.paths << Rails.root.join('spec/karma')

参考

http://codetunes.com/2014/karma-on-rails/ https://github.com/monterail/rails-angular-karma-example