【发布时间】:2017-04-19 19:11:44
【问题描述】:
在 Capybara 注册后,我需要测试特定的智能重定向流程。假设我的网站中有几个interesting_pages,我想在确认注册后将用户重定向到最后访问的有趣页面
小场景:
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"
对于实际实现,我决定选择controller.session[]= 作为suggested in this answer。在呈现page "42" 的控制器中,我将会话设置为session[:interesting_page] = request.original_url
在开发过程中,单击电子邮件中的确认链接后,我可以在设计确认期间成功重定向到 session[:interesting_page]
但是,当我尝试使用 Cucumber 进行测试时,Capybara-email 在单击电子邮件链接时似乎会重置会话,因此当我单击电子邮件中的链接以重新连接时,session[:interesting_page] 会被删除...
编辑When I sign up and I confirm via the link in the email 步骤基本上是通过设计注册控制器,我使用 Capybara::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
【问题讨论】:
-
显示您的
When I sign up and I confirm via the link in the email步骤 -
还有
When I visit the interesting page "42"步骤 - 重要的是这些步骤最后有预期,以确保页面实际加载。如果没有这些期望,则会话 cookie 可能永远不会在浏览器中设置。 -
@ThomasWalpole 我刚刚添加了步骤代码。此外,我通过 cucumber 进行了检查,尝试在电子邮件步骤之前加载其他页面,并且确实设置了会话。
标签: ruby-on-rails session devise cucumber capybara