【问题标题】:Rails- Testing Devise. reset password.Opening link in mailRails-测试设计。重置密码。在邮件中打开链接
【发布时间】:2015-12-14 17:03:15
【问题描述】:

我正在尝试测试用于重置密码的设计功能,但在尝试测试邮件中的链接和访问链接到的页面时遇到了一些问题。

我尝试了两种方法都没有成功:

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

如果我这样做,它会将我带到初始页面:

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

【问题讨论】:

  • reset_password_token 后:我有一个 user2.reset_password_token)
  • 第二个例子可以理解。它需要通过邮件并单击邮件中的重置密码链接。我建议你使用letter openermail catcher
  • 你为什么要测试这个@Westey?设计已经过测试和维护。我想你不应该担心这样的测试。
  • 我正在使用 mailcatcher,一切正常,但我无法在测试环境中访问该页面(介绍新密码的页面)。由于网站性质的特殊原因,我需要添加额外的安全性和额外的测试:S

标签: ruby-on-rails testing rspec devise mailer


【解决方案1】:

解决了! 邮件中有更多链接。 我选择我想要的方法是使用 links_in_email(email)。 我用 pry 来检查数组中的链接 (links_in_email(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

【讨论】:

    猜你喜欢
    • 2012-08-29
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 2017-04-10
    • 1970-01-01
    • 2013-09-15
    • 2016-04-04
    相关资源
    最近更新 更多