fez
fez

Reputation: 1835

Using regular expressions with Capybara

In the below capybara test I want to check that a user can see any number after 'Temperature:' when they visit the root path of my rails application. The value which is stored in the instance variable @temperature is retrieved from an API and is displayed to the user when the page is refreshed.

How can i put a ruby regular expression like 'Temperature: \d' into my spec below?

Spec:

require "rails_helper"
feature "user sees temperature" do
  scenario "success" do
    visit root_path
    expect(page).to have_css 'p', text: 'Temperature: \d'
  end
end

View:

<p>Temperature: <%= @temperature %></p>

Upvotes: 1

Views: 740

Answers (1)

ndnenkov
ndnenkov

Reputation: 36101

This is about right, you just have to use regex syntax (//) instead of string one (''):

expect(page).to have_css('p', text: /Temperature: \d/)

Upvotes: 1

Related Questions