Capybara & Rspec: How to delete an account?

三世轮回 提交于 2019-12-06 08:46:31

问题


I'm using Devise and writing a test for the scenario of a user deleting their own account but I'm stuck on how I would call up the confirm box and click OK.

Here is the link and my test:

<p><%= link_to "Delete my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete %></p>

spec/requests/users_spec.rb

scenario 'user deletes account' do
   make_user_and_login
   click_link('Account Settings')
   page.should have_selector('title', :text => 'Account Settings')
   click_link('Delete my account')
   # Are You Sure?
   # click OK in confirm box
   # page.should etc.....
end

How would this be done?


回答1:


Make sure capybara is using a driver which supports javascript. Then try this:

page.driver.browser.switch_to.alert.accept

Alternately, to cancel:

page.driver.browser.switch_to.alert.dismiss



回答2:


Try

page.evaluate_script('window.confirm = function() { return true; }')

this should work, then check for something like

page.should have_content "Account deleted"



回答3:


As @suweller said, we all were using the Capybara::RackTest default driver. However, without changing any settings and adding :js => true in my rspec tests I got them to pass, when before I got the same error as you were getting.

This allows me to then use page.driver.browser.switch_to.alert.accept at least.



来源:https://stackoverflow.com/questions/12334294/capybara-rspec-how-to-delete-an-account

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!