Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara testing javascript failure to refresh .count

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

like image 369
randomor Avatar asked Nov 20 '25 12:11

randomor


1 Answers

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').
like image 164
Ari Avatar answered Nov 23 '25 06:11

Ari