Search code examples
ruby-on-railsrspecrspec-rails

Cannot get 'admin-delete-user-account' test to pass


I have created a function where an admin can delete a user account on their dashboard. This function works successfully in practice.

(I have deleted user accounts through the admin dashboard and have confirmed with rails console.)

However, I cannot get an rspec test to pass testing this function. It always returns that the user account is still present.

The form on the site:

<%= form_for @user, url: admin_user_path(@user), data: { confirm: 'Are you sure?' }, html: { method: :delete } do |f| %>
  <%= f.submit "Delete account", class:"danger", data: { test: "delete-user-account" } %>
<% end %>

Test:

require 'rails_helper'

RSpec.feature 'deleting users' do
  let!(:admin) { create :admin }

  scenario 'successfully deleted an external user account' do
    sign_in admin, scope: :admin

    user = create(:user)
    visit admin_user_path(user)

    find('[data-test="delete-user-account"]').click

    visit root_path
    expect(user).not_to be_present
  end
end

Solution

  • Because you need to accept the alert "Are you sure?"

    If you use the Selenium API you can do:

    find('[data-test="delete-user-account"]').click
    
    page.driver.browser.switch_to.alert.accept
    
    visit root_path
    expect(user).not_to be_present