【问题标题】:Rails Minitest with Capybara, how get assert_select "button", "label" to work correctly?带有 Capybara 的 Rails Minitest,如何让 assert_select "button"、"label" 正常工作?
【发布时间】: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


【解决方案1】:

你想要

assert_selector :button, "Update Account"

但您还需要使用 Capybara 方法进行会话控制,visit 等 -- Capybara 不使用来自集成测试的 getpost 等响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 2020-08-27
    • 2018-06-11
    相关资源
    最近更新 更多