Capybara电子邮件重置会话 [英] Capybara email resets the session

查看:199
本文介绍了Capybara电子邮件重置会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Capybara注册后测试智能重定向的具体流程。假设我的网站有几个 interesting_pages ,我想在确认注册后将用户重定向到最后访问的有趣页面。



小情景:

 当我访问有趣的页面42
我访问了一些不感兴趣的页面
#注意在访问其他页面时,会话[:interesting_page]正确设置为页面42的网址
当我注册并通过电子邮件中的链接确认
#在这一点上,我已经失去了会话[:interesting_page]
然后我应该被确认并重定向到最后访问的有趣的页面42

对于实际的实现,我决定选择 controller.session [] = 作为在此答案中建议。在页面42的控制器中,我将会话设置为 session [:interesting_page] = request.original_url



在开发过程中,我可以在设计确认后点击电子邮件中的确认链接成功地重定向到会话[:interesting_page]



然而,当我尝试使用Cucumber测试这个时,Capybara电子邮件似乎重新启动会话,点击电子邮件链接,所以 session [:interesting_page] 当我点击电子邮件中的链接重新连接时删除...



编辑当我注册并通过电子邮件中的链接进行确认步骤基本上是通过设计注册控制器,我使用Capybara ::电子邮件点击确认电子邮件

 #嵌套的步骤为当我注册,我通过电子邮件中的链接确认
我填写电子邮件与$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ !
那么我应该看到请通过链接确认您的帐户!
#在这个阶段会话[:interesting_page]仍然存在并指向页面42
并且一封电子邮件应该被发送到#{user.email}
在当前mail我点击Je finalize mon inscription

#我使用的RegistrationController代码类似于devise并返回一个pending_confirmation页面
#respond_with resource,location:pending_confirmation_path


#相关的步骤执行
然后(/一个电子邮件应该被发送到([^] *)/)do | address |
open_email(address)
expect(current_email).to
end

当(/在当前邮件中单击([^] *)$ /)do | link |
current_email.click_link link
end


解决方案

p> Capybara中的动作可以异步发生。这意味着如果您不检查页面上应该可见的内容,一旦您执行的任何操作都已完成,并调用另一个访问或操作,那么由该操作响应设置的任何Cookie可能不会得到设置,这意味着你实际上没有登录,或者保存了一些重要的数据。在你的情况下,这意味着(至少)我点击注册!,您需要对成功注册后才会出现在页面上的文本的期望。



另一件事是检查电子邮件中的网址的主机名(可能是端口)是否与正常访问中使用的名称匹配。


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

解决方案

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.

这篇关于Capybara电子邮件重置会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆