Using Capybara testing javascript alert in rspec. Why
expect{
click_link "Cancel my account"
page.driver.browser.switch_to.alert.accept
}.to change(User, :count).by(-1)
fails. Yet
puts User.count
expect{
click_link "Cancel my account"
page.driver.browser.switch_to.alert.accept
puts User.count
}.to change(User, :count).by(-1)
didn't? How do I fix the test? There is no reload function for User
Since you're using a javascript spec, the browser/JS is running in a different process from your test, and Capybara doesn't know to wait until the browser request completes before continuing, since you haven't issued any Capybara command after accepting the alert. I'm guessing the puts adds just enough of a time delay for the actual database to change. If you want to wait for Capybara to return until the next page starts to get loaded (and thus the database has been updated), you can do something like this at the end of the block:
page.find('head').
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With