在 Rails 3.x 功能测试中使用 Capybara 的正确方法是什么?
因此,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不会使用 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.
Capybara 提供了一些非常好的语法,也可以在功能测试中使用。您只需要包装页面函数即可返回 Capybara::Node::Simple 包装的 @response.body。
来自 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.
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