Levi
Levi

Reputation: 175

Capybara escaping regex

I'm trying to write a test in a Rails application using Capybara that includes this:

expect(page).to have_content(text: /\bEdit\b/)

I need the word boundaries on the string because other content on the page will match without it (for example, the word "Editorial").

When I run that, I get a failure with the following message:

expected to find text "{:text=>/\\bEdit\\b/}" ...

Note the double escaping on the \b's!

How do tell Capybara not to escape my backslashes in my regex?

Upvotes: 0

Views: 457

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49880

Capybara's have_content (alias of have_text) takes a string or a regexp - not a :text option. You want

expect(page).to have_content(/\bEdit\b/)

Note: current versions of Capybara should have raised an error telling you that :text was not a valid option for the have_content matcher, so I'm not sure how old a version of Capybara you are running.

Upvotes: 2

Related Questions