2015-12-14 90 views
0

我正在尝试重新设置密码的设备功能,但我在尝试测试邮件中的链接并访问链接的页面时遇到了一些问题。Rails-测试设计。重置密码。在邮件中打开链接

我曾尝试2种方法来做到这一点:

1)

scenario "User resets his password" do 
    user2 = FactoryGirl.create(
    :user, reset_password_token: "new_password", 
) 

    visit new_user_password_path 
    fill_in "Email", with: user2.email 
    click_on "Send me reset password instructions" 

    open_email(user2.email) 
    click_first_link_in_email 
    expect(page).to have_content("Reset your password") 

end 

如果我这样做,我得到的错误:

Failure/Error: click_first_link_in_email 

ActionController::RoutingError: 
    No route matches [GET] "/1999/xhtml'" 

2)

scenario "User resets his password" do 
    user2 = FactoryGirl.create(:user, reset_password_token: "new_password") 
    visit edit_user_password_path(reset_password_token: 
    expect(page).to have_content("Reset your password") 
    end 

如果我这样做, l页说:

"You can't access this page without coming from a password reset email" 
+0

reset_password_token后:我有一个 user2.reset_password_token) – Wesley

+0

第二个例子是可以理解的。它需要通过邮件并单击邮件中的重置密码链接。我会建议你使用[letter opener](https://github.com/ryanb/letter_opener)或[邮件捕获器](https://github.com/sj26/mailcatcher) – CallmeSurge

+0

你为什么要测试这个@Westey?设计已经过测试和修补。我想你不应该担心这种测试。 – Guido

回答

0

解决! 邮件内有更多链接。 我选择我想要的方式是使用links_in_email(电子邮件)。 我用撬检查阵列中的链接(links_in_email(电子邮件),然后选择一个,我很感兴趣,

scenario "User resets his password" do 
    visit new_user_password_path 
    fill_in "Email", with: @user.email 
    click_on "Send me reset password instructions" 

    mail = ActionMailer::Base.deliveries.last 
    link = links_in_email(mail)[1] 

    visit link 
    expect(page).to have_content("Reset your password") 
end