Ruby on rails 为什么rspec不使用密码重置令牌更新数据库记录?
在运行rails测试时,我遇到了RSpec的一些问题。测试如下:Ruby on rails 为什么rspec不使用密码重置令牌更新数据库记录?,ruby-on-rails,testing,rspec,Ruby On Rails,Testing,Rspec,在运行rails测试时,我遇到了RSpec的一些问题。测试如下: it "allows a user to request a password reset" do user = FactoryGirl.create(:user, :name => "John Smith", :email => "john@test.org") identity = Identity.create!(email: user.email, password: "please", passwor
it "allows a user to request a password reset" do
user = FactoryGirl.create(:user, :name => "John Smith", :email => "john@test.org")
identity = Identity.create!(email: user.email, password: "please", password_confirmation: "please")
visit "/"
click_on "Forgotten password?"
fill_in "email", :with => "john@test.org"
click_on "Reset Password"
ActionMailer::Base.deliveries.last.to.should include(user.email)
ActionMailer::Base.deliveries.last.body.should include(identity.password_reset_token)
visit '/password_resets/' + identity.password_reset_token.to_s + '/edit'
^ **** ERROR HERE : password_reset_token is nil! ****
fill_in "identity_password", :with => "newpass123"
fill_in "identity_password_confirmation", :with => "newpass123"
click_on "Update Password"
within page.all('div#loginModal')[0] do
fill_in "auth_key", :with => "john@test.org"
fill_in "password", :with => "newpass123"
click_button "Log in"
end
page.should have_content("Hi John")
end
你可以看到我概述了我要返回的错误,这很奇怪,因为当我检查发送的电子邮件时,它实际上生成了密码重置令牌
作为参考,我使用的代码是:
class PasswordResetsController < ApplicationController
def create
identity = Identity.find_by_email(params[:email])
identity.send_password_reset if identity
redirect_to root_url, :notice => "Email sent with password reset instructions."
end
def edit
@identity = Identity.find_by_password_reset_token!(params[:id])
end
def update
@identity = Identity.find_by_password_reset_token!(params[:id])
if @identity.password_reset_sent_at < 2.hours.ago
redirect_to new_password_reset_path, :alert => "Password reset has expired."
elsif @identity.update_attributes(params[:identity])
redirect_to root_url, :notice => "Password has been reset."
else
render :edit
end
end
end
类密码重置控制器“已发送带有密码重置说明的电子邮件。”
结束
定义编辑
@identity=identity.find_by_password_reset_token!(参数[:id])
结束
def更新
@identity=identity.find_by_password_reset_token!(参数[:id])
如果@identity.password\u reset\u在<2小时前发送
将\u重定向到新的\u密码\u重置路径,:alert=>“密码重置已过期。”
elsif@identity.update_属性(参数[:identity])
将\重定向到根\ url,:notice=>“密码已重置。”
其他的
渲染:编辑
结束
结束
结束
而且
class Identity < OmniAuth::Identity::Models::ActiveRecord
attr_accessible :email, :password, :password_confirmation
validates_uniqueness_of :email
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
def send_password_reset
generate_token(:password_reset_token)
self.password_reset_sent_at = Time.zone.now
self.save!
UserMailer.password_reset(self).deliver
end
private
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while Identity.exists?(column => self[column])
end
end
class-Identity/^[-a-z0-9\+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
def发送密码重置
生成\u令牌(:密码\u重置\u令牌)
self.password\u reset\u sent\u at=Time.zone.now
自救!
UserMailer.password\u重置(self.deliver)
结束
私有的
def生成_令牌(列)
开始
self[column]=SecureRandom.urlsafe_base64
标识存在时结束?(列=>self[column])
结束
结束
有人能提出为什么会发生这种情况吗?这是否与数据库记录没有保存/访问或缓存在测试环境中的某个位置有关
感谢您的帮助。只是一个猜测:您不需要更新您的标识变量(如通过电子邮件查找),以便使用新参数更新对象吗? 在访问“密码重置”之前,请尝试使用 身份。通过电子邮件查找(user.email)
您可能会遇到时间问题。在
单击“重置密码”
步骤后,在进入访问“重置密码…”
步骤之前,检查页面是否返回预期值。是的,就是这个步骤!我假设它会被更新,但这大概是因为我没有使用事务性装置?