我需要在注册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
在开发中,我在点击电子邮件中的确认链接后,在设计确认期间成功地能够redirect_to session [:interesting_page]
然而,当我尝试使用Cucumber进行测试时,Capybara-email似乎在点击电子邮件链接时会重置会话,因此当我点击电子邮件中的链接重新连接时,session[:interesting_page]
会被删除... < / p>
编辑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
答案 0 :(得分:2)
Capybara中的操作可以异步发生。这意味着,如果您不检查页面上应该显示的内容,一旦您执行的任何操作完成,并调用另一个访问或操作,那么响应该操作所设置的任何Cookie可能没有设置,这意味着您实际上没有登录,或者保存了一些重要数据。在您的情况下,这意味着在(至少)ALTER DATABASE SCOPED CONFIGURATION SET IDENTITY_CACHE=OFF ;
之后您需要对成功注册后出现在页面上的文本的期望。
要检查的另一件事是电子邮件中网址的主机名(可能还有端口)与正常访问所使用的网址相匹配。