Cameron Bass Cameron Bass - 7 months ago 26
Ruby Question

Calling RSpec sleep

I'm writing a feature test right now that is trying test an edit action. It's a bit strange to me because it's not seeing what I expect when I run RSpec normally but when I use save_and_open_page command it shows that what I'm expecting is actually there in the browser. I think this is a selenium issue but I'm new to rspec and I'm not sure. I've been suggested to use "Sleep" or "Pause" but I'm not sure how to use these methods and I can't find any good docs on it. I'm wondering if someone can show me a valid way to use this on my current code?

TEST

require "rails_helper"

RSpec.feature "Edit 'Bounce Back' Message", :type => :feature do
given!(:group) { Group.create!(name: "Group A", response: "You are now subscribed for updates") }

scenario "Staff can see the response messages" do
visit "/groups"

user = FactoryGirl.create(:user)
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"

expect(page).to have_content("You are now subscribed for updates")
expect(page).to have_content("Group a")
end
scenario "Staff can edit the response messages" do
visit "/groups/#{group.id}/edit"

user = FactoryGirl.create(:user)
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"

expect(page).to have_content("You have now subscribed for updates")
end
end


I hope this is enough info please let me know if you need more. Thank You!
I've also tried using rspec_wait but it still is sending an error. here it is

enter image description here

Answer

Instead of using hardcoded sleeps and delays, look into using rspec-wait helper that provides you a way to wait for a specific condition before proceeding. In your case:

expect(page).to have_content("You have now subscribed for updates")

would transform into:

wait_for(page).to have_content "You have now subscribed for updates"