我最近将申请从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