【问题标题】:Why is rspec not updating the database record with a password reset token?为什么 rspec 不使用密码重置令牌更新数据库记录?
【发布时间】:2013-01-23 13:23:34
【问题描述】:

在运行 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", 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

你可以看到我已经概述了我要返回的错误,这很奇怪,因为当我检查发送的电子邮件时,它确实会生成一个 password_reset_token。

有关信息,我用来执行此操作的代码是:

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

还有……

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

谁能说明为什么会发生这种情况?是否与我的测试环境中某处未保存/访问或缓存数据库记录有关?

任何帮助表示赞赏。

【问题讨论】:

    标签: ruby-on-rails testing rspec


    【解决方案1】:

    只是一个猜测:您是否需要更新您的身份变量,例如通过电子邮件查找它,以便使用新参数更新对象? 在访问“password_resets”之前,请尝试使用

    加载身份

    Identity.find_by_email(user.email)

    【讨论】:

    • 是的,就是这个!我以为它会更新,但大概这与我没有使用事务性固定装置的事实有关?
    【解决方案2】:

    您可能会遇到时间问题。在click_on 'Reset Password' 步骤之后,检查页面是否返回预期值,然后再转到visit 'passwords_reset...' 步骤。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      相关资源
      最近更新 更多