解决方案
感谢Steven Harman提供的代码片段,我已经使其正常工作。 devise_mail_helpers.rb
module Features
module MailHelpers
def last_email
ActionMailer::Base.deliveries[0]
end
# Can be used like:
# extract_token_from_email(:reset_password)
def extract_token_from_email(token_name)
mail_body = last_email.body.to_s
mail_body[/#{token_name.to_s}_token=([^"]+)/, 1]
end
end
end
我将文件devise_mail_helpers.rb
添加到与功能规格相同的文件夹中,并编写了此规格。
require 'devise_mail_helpers.rb'
include Features
include MailHelpers
describe "PasswordResets" do
it "emails user when requesting password reset" do
user = FactoryGirl.create(:user)
visit root_url
find("#login_link").click
click_link "Forgot your password?"
fill_in "Email", :with => user.email
click_button "Send instructions"
current_path.should eq('/users/sign_in')
page.should have_content("You will receive an email with instructions about how to reset your password in a few minutes.")
last_email.to.should include(user.email)
token = extract_token_from_email(:reset_password) # Here I call the MailHelper form above
visit edit_password_url(reset_password_token: token)
fill_in "user_password", :with => "foobar"
fill_in "user_password_confirmation", :with => "foobar1"
find('.signup_firm').find(".submit").click
page.should have_content("Password confirmation doesn't match Password")
end
end
这个问题可以通过查看下面Dave的回答来解决。如果要在浏览器中运行,请参考相关规范。
原始问题
在我的Rails 4应用程序中,我已经将devise升级到3.1并运行了rails s
,然后我得到了这个错误:
`raise_no_secret_key': Devise.secret_key was not set.
Please add the following to your Devise initializer: (RuntimeError)
config.secret_key = '--secret--'
我在devise的初始化程序中添加了密钥。
之后,当我尝试重置密码时,出现以下错误:
Reset password token is invalid
看起来电子邮件中发送的令牌不正确。其他一切正常。我像刀子一样轻松地登录和退出。
更新
现在我猜测问题可能与reset_password_token
的加密有关。从功能规范中可以看到:
user = FactoryGirl.create(:user,
:reset_password_token => "something",
:reset_password_sent_at => 1.hour.ago)
visit edit_password_url(user, :reset_password_token =>
user.reset_password_token)
fill_in "user_password", :with => "foobar"
click_button "Change my password"
page.should have_content("Password confirmation doesn't match Password")
发生的错误是:
Failure/Error: page.should have_content
("Password confirmation doesn't match Password")
expected to find text "Password confirmation doesn't match Password" in
"Reset password token is invalid"
有任何想法我漏掉了什么吗?