← Back to Upcase

Testing for count of text content

(Jon Seidel) #1

I have a bug in my code wherein a flash message is rendered twice, so I want to create a scenario to test for that and then fix it. How can I define this test? I’ve tried several things in feature and controller specs and so far no luck.

(Giuseppe Modarelli) #2

Hello JESii,

you could try this with capybara

expect(page).to have_selector("div.alert", count: 1)

where div.alert you your flash element on the page.

(Jon Seidel) #3

Terrific! Thanks very much, @gmodarelli That didn’t work directly because my flash messages came in with different classes:

  <div class="flash-error">Sign in unsuccessful! (1)</div>
  <div class="flash-notice">Sign in unsuccessful</div>

Howver, you definitely got me going in the right direction; what worked is:

expect(page).to have_text("unsuccessful", count: 1)

Test is now failing as expected.

(Giuseppe Modarelli) #4

Your welcome :wink: