开发者

JavaScript Unit Testing in Rails 3.1

开发者 https://www.devze.com 2023-03-11 19:46 出处:网络
I am wondering what is the easiest way to do the JavaScript Unit testing as part of Rails 3.1 application.

I am wondering what is the easiest way to do the JavaScript Unit testing as part of Rails 3.1 application.

I like Jasmine a lot and it works pretty well (although some tricks are required for it to pick up .coffee files).

The only problem I have with Jasmine is that it runs all the tests examples inside one huge page which is very problematic as it requires loa开发者_如何学Goding ALL of the scripts.

The thing I really want is Jasmine + multiple test suites in multiple files (so that it generates multiple html files including spec files).

In addition to that, I want to run tests (hopefully easily) in the browsers, headless or inside a JS engine (when possible).

Any recommendations?


Teaspoon does pretty much what you're looking for.

I wrote most of it, and it's based on my experience with writing javascript specs and using Rails 3.1 / coffeescript. Your question includes some of the very things that made me want to contribute it in the first place.

EDIT:

To clarify, Teaspoon supports defining multiple suites, has a rake task, supports using Selenium Webdriver or PhantomJS as drivers, Jasmine, Mocha, or QUnit test frameworks, allows running from the command line (eg. bundle exec teaspoon spec/javascripts/my_spec.coffee), and several other nice to haves.


Where i work, we wanted to find a solution to cover pretty much what you are mentioning.

We examined the following frameworks:

  • teaspoon
  • konacha
  • karma
  • bunyip
  • jasmine-rails

We finally picked teaspoon. It required minimal setup and it was easy to integrate with our CI. It provides suites, asset pipeline support (so that you can test .coffee without hacks) and it can run in RAILS_ENV=test


You might want to try the evergreen (https://github.com/jnicklas/evergreen). It allows you to write testcases with jasmine and run your tests in the browsers, headless or inside a JS engine.

You can found the usage of this gem on the readme section https://github.com/jnicklas/evergreen#readme

Unfortunately, evergreen doesn't play well with new rails 3.1 feature yet (at the time this answer is made). So I try to create some monkey patch to get it play well.

# config/evergreen.rb
unless defined?(CONFIG_EVERGREEN_LOADED)
  CONFIG_EVERGREEN_LOADED = true

  require ::File.expand_path('../environment',  __FILE__)

  unless "".respond_to?(:each) # this monkey patch make the old capybara play well with ruby 1.9.2
    String.class_eval do
      def each &block
        self.lines &block
      end
    end
  end

  module Evergreen

    class << self
      def application_with_additions(suite)
        app = application_without_additions(suite)

        app.map "/assets" do
          assets = Rails.application.config.assets
          if assets.enabled
            require 'sprockets'
            sprockets = Sprockets::Environment.new(suite.root)
            sprockets.static_root = File.join(suite.root, 'public', assets.prefix)
            sprockets.paths.concat assets.paths
            sprockets.js_compressor = nil
            run sprockets
          end
        end
        app
      end

      alias_method :application_without_additions, :application
      alias_method :application, :application_with_additions
    end

end


As of now, I haven't found a reasonable answer to this. But the issue #24 of jasminerice is probably the closes to the answer if it will be implemented


Perhaps try jasmine-headless-webkit. This offers the capability to run your Jasmine specs within a headless WebKit browser.

0

精彩评论

暂无评论...
验证码 换一张
取 消