Skip to content

Instantly share code, notes, and snippets.

@dogweather
Last active December 11, 2024 16:51
Show Gist options
  • Save dogweather/b9a5c7d27792b77677e27747acf3d2f4 to your computer and use it in GitHub Desktop.
Save dogweather/b9a5c7d27792b77677e27747acf3d2f4 to your computer and use it in GitHub Desktop.
Use CSS Selectors in RSpec Request Specs
def css(selector)
html.css(selector).text
end
def html
Nokogiri::HTML(response.body)
end
# How to use the above helpers.
RSpec.describe 'Test some page', type: :request do
it 'has a <p> with "Title"' do
get '/my-page'
expect( css 'p' ).to include 'Title'
end
end
@stepchud
Copy link

stepchud commented Dec 11, 2024

thanks for the solution!
NB: you can include the Capybara::RSpecMatchers in your RSpec.configure block (mine is in rails_helper.rb) like this:

RSpec.configure do |config|
  config.include Capybara::RSpecMatchers, type: :request
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment