在 Rails 3.x 功能测试中使用 Capybara 的正确方法是什么?

发布于 2025-01-07 11:18:41 字数 782 浏览 3 评论 0原文

因此,Rails 会在 test/function 目录中为控制器生成一些功能测试。这些测试是从 ActionController::TestCase 扩展的。

但在 Capybara 的网站中,他们只展示了如何通过猴子修补 ActionDispatch::IntegrationTest 来准备集成测试:

DatabaseCleaner.strategy = :truncation

class ActionDispatch::IntegrationTest
  # Make the Capybara DSL available in all integration tests
  include Capybara::DSL

  # Stop ActiveRecord from wrapping tests in transactions
  self.use_transactional_fixtures = false

  teardown do
    DatabaseCleaner.clean # Truncate the database
    Capybara.reset_sessions! # Forget the (simulated) browser state
    Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver
  end
end

但他们没有提到如何设置 Capybara 以用于功能测试。这样做的正确方法是什么?

So Rails generates some functional tests for controllers in the test/functional directory. These tests are extending from ActionController::TestCase.

But in Capybara's web site, they only show how to prepare the integration tests by monkey patching ActionDispatch::IntegrationTest:

DatabaseCleaner.strategy = :truncation

class ActionDispatch::IntegrationTest
  # Make the Capybara DSL available in all integration tests
  include Capybara::DSL

  # Stop ActiveRecord from wrapping tests in transactions
  self.use_transactional_fixtures = false

  teardown do
    DatabaseCleaner.clean # Truncate the database
    Capybara.reset_sessions! # Forget the (simulated) browser state
    Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver
  end
end

But they don't mention how to setup Capybara to be used with functional tests. What's the correct way to do that?

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

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

发布评论

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

评论(2

归属感 2025-01-14 11:18:41

您不会使用 Capybara,因为那是网页工具。功能测试不涉及这一点。您只需要证明您的控制器逻辑具有正确的行为即可。为此,RSpec 就足够了。

请参阅 http://rubydoc.info/gems/rspec-rails/frames 控制器规格部分。

You wouldn't use Capybara, because that is a tool for Web pages. Functional testing does not involve that. You just have to prove that your controller logic has the right behaviour. For that RSpec is enough.

See http://rubydoc.info/gems/rspec-rails/frames Controller Specs section.

乱世争霸 2025-01-14 11:18:41

Capybara 提供了一些非常好的语法,也可以在功能测试中使用。您只需要包装页面函数即可返回 Capybara::Node::Simple 包装的 @response.body。

class Test::Unit::TestCase
  def page
    Capybara::Node::Simple.new(@response.body)
  end
end

来自 Capybara rdoc:

{Capybara::Node::Simple} 是 {Capybara::Node::Base} 的更简单版本,其中仅包含 {Capybara::Node::Finders} 和 {Capybara::Node::Matchers } 并且不包括 {Capybara::Node::Actions}。

莫信息:

http://robots. Thoughtbot.com/post/8087279685/use-capybara-on-any-html-fragment-or-page

Capybara provides some really nice syntax which can also be used in functional tests. You just need to wrap the page function to return a Capybara::Node::Simple wrapped @response.body.

class Test::Unit::TestCase
  def page
    Capybara::Node::Simple.new(@response.body)
  end
end

From the Capybara rdoc:

A {Capybara::Node::Simple} is a simpler version of {Capybara::Node::Base} which includes only {Capybara::Node::Finders} and {Capybara::Node::Matchers} and does not include {Capybara::Node::Actions}.

Mo info:

http://robots.thoughtbot.com/post/8087279685/use-capybara-on-any-html-fragment-or-page

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