【问题标题】:'Password digest missing on new record' when i test validation on my user_spec.rb for uniqueness of fields当我在我的 user_spec.rb 上测试验证字段的唯一性时,“新记录中缺少密码摘要”
【发布时间】:2013-07-13 23:45:10
【问题描述】:

我已经为此苦苦挣扎了几个小时,但在任何地方都找不到答案。

基本上我正在使用 shoulda 为我的模型编写规范,出于某种原因,即使我的 let 工作正常并且实际应用程序工作正常(手动确认并使用请求规范),如果我包括:

it { expect(user).to validate_uniqueness_of :email }

在我的描述块中,我会收到以下错误:

1) User attributes should require case sensitive unique value for email
 Failure/Error: it { expect(user).to validate_uniqueness_of :email }
 RuntimeError:
   Password digest missing on new record
 # ./spec/models/user_spec.rb:14:in `block (3 levels) in <top (required)>'

我已经到了解决这个规范阻碍我前进的地步,这绝不是一件好事,因为实施已经奏效了。

如果有人可以在这里帮助我,那就太好了,因为我真的不想为了让事情进展顺利而开始跳过那些由于某种不明原因而失败的测试。

我觉得 rspec 好像“忽略”了调用 has_secure_password 以保存摘要的 before 方法,但我不确定。我只是假设,因为如果请求规范运行良好,这是在做我不知道的事情。

我正在使用 rails 4.0.0

以下是与此相关的代码。我很感激任何帮助。谢谢

user_spec.rb

require  'spec_helper'

describe User do

  let(:user) { 
    user = User.new(:email => 'example@example.com', 
                    :username => 'theo', 
                    :password => 'secretpass',
                    :password_confirmation => 'secretpass')}

  describe "attributes" do

    it { expect(user).to validate_presence_of :email } 
    it { expect(user).to validate_uniqueness_of :email }

    it { expect(user).to validate_presence_of :username }
    it { expect(user).to validate_uniqueness_of :username }

    it "saves are saved" do
      user.save!
      expect(user).to be_valid
    end

  end  


end

用户.rb

class User < ActiveRecord::Base
  has_secure_password


  validates :email, :username, presence: true, uniqueness: true
  validates :password, presence: true, :on => :create


end

users_controller.rb

class UsersController < ApplicationController
  def index
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)

    if @user.save
      redirect_to root_path, :notice => 'You have successfully signed up.'
    else
      render 'new'
    end
  end

  private

  def user_params
    params.require(:user).permit(:email, :username, :password, :password_confirmation)
  end
end

【问题讨论】:

  • 您的测试数据库的users 表中是否存在password_digest 列?
  • 列存在,是的。 :(

标签: ruby-on-rails rspec ruby-on-rails-4 shoulda


【解决方案1】:

请确保您的用户架构中存在password_digest,详情请参阅here

create_table “users”, force: true do |t|
    ……
    t.text     “password_digest”
    …..
end

如果它在您的开发中有效但无法测试,请检查您是否在测试环境中执行 rake db:migration,我的意思是 RAILS_ENV=test rake db:migrate


!!!已更新 -- 与 shouda-matchersgocha 相关

这似乎是shoulda-matchers 的错误,因为当你这样做时,测试将通过:

it "should be validate uniq of email " do
    user.save
    expect(user).to validate_uniqueness_of :email
end

我之所以这样做user.save是因为this ,否则它会为你创建一条记录,这将导致你得到的错误:

# https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb#L126
 def create_record_in_database(options = {})
      if options[:nil_value]
        value = nil
      else
        value = "arbitrary_string"
      end

      @subject.class.new.tap do |instance|
        instance.send("#{@attribute}=", value)
        instance.save(:validate => false) # the error happens here, not in your code
      end
    end

所以最重要的是,这可能是shouda-matchers 的一个错误,虽然我没有时间弄清楚如何修复它,现在你可以使用这种方法作为解决方法,或者使用其他方法来解决测试一下。

希望对你有帮助:-)

【讨论】:

  • 29 分贝,也没有解决。我尝试在测试环境的控制台上添加一条新记录,它工作正常。由于某种原因,它只在测试中失败。 ://
  • @TheoFelippe 你能把it { expect(user).to validate_presence_of :email }注释掉,看看是只有这个测试失败还是全部失败?
  • Mike Li,我已经这样做了。 present_of 是唯一失败的。目前我已将它们注释掉,并按应有的方式通过。
  • 您遇到了什么失败错误?相同 ? Password digest missing on new record
  • 是的,正是我在上面发布的。 ://
【解决方案2】:

可能是 shoulda-matchers 中的一个错误,可能与这个 https://github.com/thoughtbot/shoulda-matchers/issues/290 相关

干杯

【讨论】:

    【解决方案3】:

    您的问题的答案在这里:https://github.com/thoughtbot/shoulda-matchers/issues/371

    简而言之,这是根本问题:

    Rails 4 版本的has_secure_password 似乎添加了一个 before_create 确保填写password_digest;然而, Rails 3 版本没有这个检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 1970-01-01
      • 2019-06-14
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      相关资源
      最近更新 更多