Encountering an issue with a Capybara spec in my Rails application that is causing errors.
Below is the relevant snippet from my view:
#show.html.erb
...
<%= button_to 'Cancel',
cancel_subscription_path,
class: 'button button--danger',
data: {
turbo_confirm: 'Are you sure you want to cancel?',
turbo_method: 'post'
}
%>
<div id="cancel_message"></div>
...
# cancel_subscription_spec.rb
require 'rails_helper'
RSpec.describe 'CancelSubscriptions', js: true do
it 'cancels an active subscription' do
email = sign_up_and_subscribe
visit subscription_path
accept_confirm do
click_button 'Cancel'
end
subscription = User.find_by(email:).current_subscription
expect(subscription.cancelled?).to be(true)
end
end
sign_up_and_subscribe
is a simple helper method used to create and subscribe users to the application, which functions correctly in other tests. The issue arises when attempting to click the cancel button after visiting subscription_path
.
The error received is:
Selenium::WebDriver::Error::UnexpectedAlertOpenError:
unexpected alert open: {Alert text : }
(Session info: chrome-headless-shell=123.0.6312.87)
I suspected misuse of accept_confirm
method, but after verifying the documentation, I believe I am using it properly.
Even after removing accept_confirm
from the spec and turbo_confirm
from the view, the error message persisted. Could Capybara be caching the page during test runs?
If necessary, more details/code can be provided upon request.