【发布时间】:2021-06-19 03:43:22
【问题描述】:
在(未启用水豚的)集成测试中,此断言有效:
assert_select "button", "Update"
对于包含以下内容的页面:
... <button name="button" type="submit" class="btn btn-primary">Update Account</button> ...
我需要 Capybara 来进行一些 IntegrationTest 测试。
当 Capybara::Minitest::Assertions 包含在内时,我如何执行相同的断言(例如,简单地断言有一个带有文本更新的按钮)?
我在 test_helper.rb 中添加了 Capybara,如下所示;现在,同样的断言现在抛出这个错误:
TypeError: no implicit conversion of String into Hash
如果我将断言的格式更改为:
assert_select "button", text: "Update Account"
它现在抛出这个失败:
expected to find select box "button" that is not disabled but there were no matches
如果我将断言的格式更改为:
assert_selector "button", text: "Update Account"
它现在抛出这个失败:
expected to find css "button" but there were no matches
# test_helper.rb
require 'capybara/rails'
require 'capybara/minitest'
class ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
include FactoryBot::Syntax::Methods
# Make the Capybara DSL available in all integration tests
include Capybara::DSL
include Capybara::Minitest::Assertions #### THIS LINE changes minitest assert_select #####
# Reset sessions and driver between tests
teardown do
Capybara.reset_sessions!
Capybara.use_default_driver
end
end
【问题讨论】:
-
不要将 Capybara 混入
ActionDispatch::IntegrationTest。您希望有一个用于“纯”集成测试的快速轻量级测试类和一个用于测试用户交互的单独类。如果您使用的是 Rails 5.17+,那么您有 ActionDispatch::SystemTestCase 正是这样做的。你甚至可以生成system tests。如果您卡在旧版本上,请创建一个ActionDispatch::IntegrationTest的子类,作为您系统测试的基础。
标签: ruby-on-rails capybara minitest