Capybara email resets the session

孤人 提交于 2019-12-12 04:47:30

问题


I need to test a specific flow of smart redirection after signing up in Capybara. Suppose I have several interesting_pages in my website, I want to redirect the user to the last visited interesting page after confirmation of registration

Small scenario :

When I visit the interesting page "42"
And I visit a couple uninteresting pages
# Note during the visit to other pages, session[:interesting_page] is correctly set to the url of page "42"
When I sign up and I confirm via the link in the email
# At this point I have lost the session[:interesting_page]
Then I should be confirmed and redirected to the last visited interesting page "42"

For the actual implementation, I decided to opt for controller.session[]= as suggested in this answer. In the controller that renders the page "42" I set the session to session[:interesting_page] = request.original_url

In development I am successfully able to redirect_to session[:interesting_page] during devise confirmation after clicking on the confirmation link in the email

However when I try to test this using Cucumber, Capybara-email seems to reset the session when clicking on an email link, so session[:interesting_page] gets removed when I click the link in the email to reconnect...

EDIT the When I sign up and I confirm via the link in the email step basically goes through a devise registration controller and I use Capybara::Email to click the confirmation email

# Nested steps for "When I sign up and I confirm via the link in the email"
I fill in "email" with ...
...
I click "Sign up !"
Then I should see "Please confirm your account via the link !"
# At this stage session[:interesting_page] still exists and points to page "42"
And an email should have been sent to "#{user.email}"
And in the current mail I click "Je finalise mon inscription"

# The RegistrationController code I use is similar to devise and returns a pending_confirmation page
# respond_with resource, location: pending_confirmation_path


# Associated step implementations
Then(/an email should have been sent to "([^"]*)"/) do |address|
  open_email(address)
  expect(current_email).to be
end

When(/in the current mail I click "([^"]*)"$/) do |link|
  current_email.click_link link
end

回答1:


Actions in Capybara can occur asynchronously. This means that if you don't check for something that should be visible on the page, once whatever action you have executed has completed, and call another visit or action then any cookies that would have been set by the response to that action may not get set, which would mean you don't actually get logged in, or have some important data saved. In your case that means after (at least) I click "Sign up !" you need an expectation for text that would appear on the page after a successful signup.

Another thing to check is that the hostname (and possibly port) of the url in the email matches the ones being used by your normal visits.



来源:https://stackoverflow.com/questions/43504182/capybara-email-resets-the-session

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