2017-04-19 70 views
0

我需要在注册后在水豚中测试智能重定向的特定流程。假设我在我的网站有几个interesting_pages,我想给用户的最后访问有趣的重定向页面注册确认后Capybara电子邮件重置会话

小场景:

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的会话[:interesting_page]点击确认链接的电子邮件

然而,当我尝试后色器件确认时测试这个使用黄瓜,水豚的电子邮件似乎重置会话邮件点击链接时,所以session[:interesting_page]当我点击电子邮件中的重新连接的链接获取删除...

编辑When I sign up and I confirm via the link in the email一步基本上经过一个设计注册控制器,我使用Capybara :: Email来点击配置文件rmation 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

显示您的'当我注册并通过电子邮件中的链接确认'步骤 –

+0

另外,当我访问有趣的页面时,步骤是重要的 - 这些步骤最终有望达到确保页面实际上已加载。如果这些期望不存在,那么会话cookie可能永远不会在浏览器中设置。 –

+0

@ThomasWalpole我刚刚添加了步骤代码。此外,我通过尝试加载其他页面之前通过电子邮件和会议确实设置黄瓜检查。 –

回答

2

在水豚中的行为可以异步发生。这意味着,如果您不检查应该在页面上显示的内容,一旦您执行的任何操作已完成,并调用另一次访问或操作,那么任何由该操作的响应设置的cookie可能不会得到设置,这意味着你实际上没有登录,或者保存了一些重要的数据。在你的情况下,这意味着(至少)I click "Sign up !"之后,你需要一个期望的成功注册后出现在页面上的文本。

另一件要检查的事情是,电子邮件中URL的主机名(可能是端口)与正常访问使用的URL相匹配。

+0

嘿,对不起,我已经有这样的事情,只是没有认为它是如此相关。谢谢你的提醒,它可能实际上解释了我在其他测试中有时看到的其他非确定性故障xD –

+0

@ CyrilDuchon-Doris - 是非常重要的,为什么'have_current_path'匹配器应该总是用'current_url'替代'eq' '。我只是添加了另一件事来检查,因为cookie是基于主机名存储的(可能依赖于浏览器的端口等) –

+0

该死!请求主机已从127.0.0.1更改为本地主机,这可能解释了不同的session_id .... 127.0.0.1是在我点击任何电子邮件之前以及它成为本地主机之前的主机名。我怎样才能统一呢? –