在 Rails 中使用 Minitest

发布于 2024-12-08 03:54:17 字数 285 浏览 0 评论 0原文

最近,我读了很多关于Minitest的文章。我真的很喜欢超轻量级测试框架的想法。我决定在最近的一个项目中用它替换 rspec,但没能让它全部正常工作。我的问题是

a) 在我的验收/集成测试中获取命名路由(rspec 和 test::unit 似乎自动包含它们,但不与 minitest 一起使用),
b) 而且 Rails 整体上缺乏采用让我感到不安(每个人似乎都在使用 rspec,尽管它更多地用于 gems/库)。

当 rspec 在测试 Rails 应用程序方面占据主导地位时,是否值得使用 minitest?

Recently, I've read quite a few articles about Minitest. I really like the idea of a super lightweight test framework. I decided to replace rspec with it in a recent project and have had no luck getting it all to work. My problems are

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

新人笑 2024-12-15 03:54:17

我是 minitest-rails 的作者。从你最初提出这个问题到现在,事情已经发生了很大的变化。我的回答假设您正在使用 minitest-rails。

命名路由

如果您使用的是 minitest-rails,那么这(现在)就可以了。您可以使用生成器来创建这些测试,或者自己编写它们。所有指定的路由都可以在您的验收/集成测试中使用。

require "minitest_helper"

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

采用

我认为,随着 Rails 4 的临近,我们将继续看到人们越来越关注将 Minitest 与 Rails 结合使用。

值得吗?

我认为现在开始 Minitest 是完全值得的。 Minitest 目前正在进行大量活动。它也与最近对快速测试的关注非常吻合。但这实际上取决于您的应用程序和团队动态。

I'm the author of minitest-rails. Things have changed a lot from the time you originally asked this to now. My answer assumes you're using 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.

乞讨 2024-12-15 03:54:17

我最近将一个应用程序从 Rspec 切换到 Minitest &这是非常值得的。测试运行速度快得多,语法鼓励更智能、更精简的代码,&不知何故,我现在对套件更有信心了(工作中的魔力减少了)。

改进延伸到集成/验收测试,我发现 Minitest with Capybara 更具可读性和可操作性。比黄瓜简单(而且脆性要小得多)。

下面是一个帮助文件,它应该是您获取单元、功能和功能所需的全部内容。使用规范语法与 Minitest 一起运行的集成测试。这是基于@tenderlove & 的要点。大量阅读/实验。注释&以下注意事项。

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

注释

  • 适合在 Rails 3.1 或 3.2 中使用。下面没试过。
  • gem 'minitest' 对于获得一些更高级的 Minitest 功能(let 块等)是必要的。
  • 这使用了 mocha(完整的模拟/存根)、迷你裙(factory_girl lite)、 &新的转弯跑步者。这些都不是依赖关系。
  • 从 Rails 3.2 开始,控制器测试中的嵌套 describe 块会引发错误

I recently switched an application from Rspec to Minitest & it was well worth it. Tests run much faster, the syntax encourages smarter, leaner code, & somehow I just have more confidence in the suite now (less magic at work).

The improvement extends to integration/acceptance testing, I find Minitest with Capybara much more readable & straightforward than Cucumber (& much less brittle).

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

Notes

  • 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
还如梦归 2024-12-15 03:54:17

最近几天我做了一些工作,使用 minitest 测试 Rails 变得更加简单。请查看http://rawonrails.blogspot。 com/2012/01/better-way-of-testing-rails-application.html 了解更多信息。

I did some work last days to make testing Rails with minitest much straightforward. Please look at http://rawonrails.blogspot.com/2012/01/better-way-of-testing-rails-application.html to find more.

嘿咻 2024-12-15 03:54:17

minitest-rails gem 使这一切变得简单。

The minitest-rails gem makes this easy.

痴情换悲伤 2024-12-15 03:54:17

Coding Ningja 的“MiniTest::Spec setup with Capybara in Rails 3.1”对于将 Minitest 与 Rails 集成有很大帮助。

http://code-ningja.posterous.com/73460416

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

http://code-ningja.posterous.com/73460416

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文