Search code examples
ruby-on-railscapybaraminitest

Rails Minitest with Capybara, how get assert_select "button", "label" to work correctly?


In a (non-capybara-enabled) IntegrationTest, THIS assertion works:

assert_select "button", "Update"

for a page containing:

... <button name="button" type="submit" class="btn btn-primary">Update Account</button> ...

I need Capybara for some of the IntegrationTest tests.

How do I perform the same assertion (e.g., simply assert there is a button with the text Update) when Capybara::Minitest::Assertions are included?

I added Capybara to test_helper.rb as shown below; now, that same assertion now throws this error:

TypeError: no implicit conversion of String into Hash

If I change the format of the assertion to:

assert_select "button", text: "Update Account" 

it now throws this failure:

expected to find select box "button" that is not disabled but there were no matches

If I change the format of the assertion to:

assert_selector "button", text: "Update Account"

it now throws this failure:

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

Solution

  • You want

    assert_selector :button, "Update Account"
    

    BUT you also need to use the Capybara methods for session control, visit, etc -- Capybara doesn't use the get, post etc responses from the integration tests.