English 中文(简体)
铁路使用小型测试
原标题:Using Minitest in Rails

最近,我刚刚读过几篇关于小型测试的文章。 我确实喜欢超重测试框架的想法。 我决定,在最近的一个项目中,用pec子取而代之,没有幸运地让它工作。 我的问题是:

a) getting named routes in my acceptance/integration tests (rspec and test::unit seem to automatically include them but no go with minitest),
b) and the overall lack of adoption in rails makes me uneasy (everyone seems to be using rspec though it s used more with gems/libraries).

Is it worth using minitest when rspec has the main dominance with testing rails applications?

最佳回答

I m the author of minitest-rails。 事情从你最初要求的时候到现在发生了很多变化。 我的回答假定,你会重新使用小型测试铁路。

Named Routes

If you are using minitest-rails this just works (now). You can use the generators to create these tests, or write them yourself. All the named routes are available in your acceptance/integration tests.

require "minitest_helper"

describe "Homepage Acceptance Test" do
  it "must load successfully" do
    get root_path
    assert_response :success
  end
end

Adoption

I think we will continue to see increased attention on using Minitest with Rails as we get closer to Rails 4.

Worth it?

I think starting with Minitest now is totally worth it. There is tremendous activity going on in Minitest right now. It aligns nicely with the recent focus on fast tests as well. But it really depends on your app and team dynamics.

问题回答

我最近将申请从Rspec转到了小型测试和安放,这是很值得的。 测试速度加快了S,辛塔克斯鼓励精彩、精炼和安放;有些东西现在我对诉讼(无工作魔力)更加信任。

这一改进延伸到融合/接受测试,我发现,与Capybara相比,小幅测试更加可读和便于使用;直截了当比Cucumber(和安插;更少的碎片)。

Below is a helper file that should be all you need to get unit, functional & integration tests running with Minitest using spec syntax. This was based on a gist by @tenderlove & a lot of reading/experimentation. Notes & caveats below.

ENV["RAILS_ENV"] = "test"
require File.expand_path( ../../config/environment , __FILE__)

require  rubygems 
gem  minitest 
require  minitest/autorun 
require  action_controller/test_case 

require  miniskirt 
require  capybara/rails 
require  mocha 
require  turn 

# Support files
Dir["#{File.expand_path(File.dirname(__FILE__))}/support/*.rb"].each do |file|
  require file
end


class MiniTest::Spec
  include ActiveSupport::Testing::SetupAndTeardown

  alias :method_name :__name__ if defined? :__name__
end


class ControllerSpec < MiniTest::Spec
  include Rails.application.routes.url_helpers
  include ActionController::TestCase::Behavior

  before do
    @routes = Rails.application.routes
  end
end

# Test subjects ending with  Controller  are treated as functional tests
#   e.g. describe TestController do ...
MiniTest::Spec.register_spec_type( /Controller$/, ControllerSpec )


class AcceptanceSpec < MiniTest::Spec
  include Rails.application.routes.url_helpers
  include Capybara::DSL

  before do
    @routes = Rails.application.routes
  end
end

# Test subjects ending with  Integration  are treated as acceptance/integration tests
#   e.g. describe  Test system Integration  do ...
MiniTest::Spec.register_spec_type( /Integration$/, AcceptanceSpec )


Turn.config do |c|
  # use one of output formats:
  # :outline  - turn s original case/test outline mode [default]
  # :progress - indicates progress with progress bar
  # :dotted   - test/unit s traditional dot-progress mode
  # :pretty   - new pretty reporter
  # :marshal  - dump output as YAML (normal run mode only)
  # :cue      - interactive testing
  c.format  = :cue
  # turn on invoke/execute tracing, enable full backtrace
  c.trace   = true
  # use humanized test names (works only with :outline format)
  c.natural = true
end

<>说明>

  • Geared for use in Rails 3.1 or 3.2. Haven t tried below that.
  • gem minitest is necessary to get some more advanced Minitest functionality (let blocks, etc.)
  • This uses mocha (fuller mocks/stubs), miniskirt (factory_girl lite), & the new turn runner. None of these are dependencies.
  • As of Rails 3.2, nested describe blocks in controller tests throw an error

过去几天,我做了一些工作,用小型测试进行铁路测试。 请看,以便找到更多信息。

minitest-rails 这样做非常容易。

Coding Ningja s "MiniTest::Spec setup with Capybara in Rails 3.1" helped a lot with integrating Minitest with Rails.





相关问题
Remove ActiveRecord in Rails 3

Now that Rails 3 beta is out, I thought I d have a look at rewriting an app I have just started work on in Rails 3 beta, both to get a feel for it and get a bit of a head-start. The app uses MongoDB ...

When will you upgrade your app to Rails 3? [closed]

Now that the Rails 3 beta is here, let s take a little straw poll. Please tell us briefly what your application does and when you will upgrade it to Rails 3. Or, if you re not planning on upgrading ...

Bundler isn t loading gems

I have been having a problem with using Bundler and being able to access my gems without having to require them somewhere, as config.gem used to do that for me (as far as I know). In my Rails 3 app, I ...

bypass attr_accessible/protected in rails

I have a model that, when it instantiates an object, also creates another object with the same user id. class Foo > ActiveRecord::Base after_create: create_bar private def create_bar Bar....

concat two fields activerecord

I m so used to oracle where you can simply concat(field1, , field2) but if I m using activerecord to find the field1 and field2, and I need a space in between, how do I accomplish this? Cheers ...

热门标签