简体   繁体   English

Rails 3.1 中的 JavaScript 单元测试

[英]JavaScript Unit Testing in Rails 3.1

I am wondering what is the easiest way to do the JavaScript Unit testing as part of Rails 3.1 application.我想知道作为 Rails 3.1 应用程序的一部分进行 JavaScript单元测试的最简单方法是什么。

I like Jasmine a lot and it works pretty well (although some tricks are required for it to pick up .coffee files).我非常喜欢 Jasmine 并且它工作得很好(尽管它需要一些技巧来获取.coffee文件)。

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 loading ALL of the scripts.我对 Jasmine 的唯一问题是它在一个巨大的页面中运行所有测试示例,这是非常有问题的,因为它需要加载所有脚本。

The thing I really want is Jasmine + multiple test suites in multiple files (so that it generates multiple html files including spec files).我真正想要的是Jasmine + 多个文件中的多个测试套件(以便它生成多个 html 文件,包括规范文件)。

In addition to that, I want to run tests (hopefully easily) in the browsers, headless or inside a JS engine (when possible).除此之外,我想在浏览器中运行测试(希望很容易),无头或在 JS 引擎中(如果可能的话)。

Any recommendations?有什么建议吗?

Teaspoon does pretty much what you're looking for. Teaspoon几乎可以满足您的需求。

I wrote most of it, and it's based on my experience with writing javascript specs and using Rails 3.1 / coffeescript.我写了大部分内容,它基于我编写 javascript 规范和使用 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.澄清一下,Teaspoon 支持定义多个套件,具有 rake 任务,支持使用 Selenium Webdriver 或 PhantomJS 作为驱动程序,Jasmine、Mocha 或 QUnit 测试框架,允许从命令行运行(例如bundle exec teaspoon spec/javascripts/my_spec.coffee ),还有其他几个不错的东西。

Where i work, we wanted to find a solution to cover pretty much what you are mentioning.在我工作的地方,我们想找到一个解决方案来涵盖您所提到的内容。

We examined the following frameworks:我们检查了以下框架:

We finally picked teaspoon.我们终于选择了茶匙。 It required minimal setup and it was easy to integrate with our CI.它需要最少的设置,并且很容易与我们的 CI 集成。 It provides suites, asset pipeline support (so that you can test.coffee without hacks) and it can run in RAILS_ENV=test它提供套件、资产管道支持(这样您就可以在没有 hack 的情况下使用 test.coffee)并且它可以在 RAILS_ENV=test 中运行

You might want to try the evergreen (https://github.com/jnicklas/evergreen).您可能想尝试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.它允许您使用jasmine编写测试用例,并在无头浏览器或 JS 引擎中运行您的测试。

You can found the usage of this gem on the readme section https://github.com/jnicklas/evergreen#readme您可以在自述文件 https://github.com/jnicklas/evergreen#readme部分找到此 gem 的用法

Unfortunately, evergreen doesn't play well with new rails 3.1 feature yet (at the time this answer is made).不幸的是,evergreen 还不能很好地与新的 rails 3.1 功能配合使用(在做出这个答案时)。 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但是茉莉花的第 24 期问题可能是接近答案的问题,如果它会实施的话

Perhaps try jasmine-headless-webkit (https://github.com/johnbintz/jasmine-headless-webkit).也许试试jasmine-headless-webkit (https://github.com/johnbintz/jasmine-headless-webkit)。 This offers the capability to run your Jasmine specs within a headless WebKit browser.这提供了在无头 WebKit 浏览器中运行 Jasmine 规范的能力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM