Rails Password digest missing on new record

xautjzd · 2013年08月21日 · 最后由 xautjzd 回复于 2013年08月21日 · 3445 次阅读

看 Ruby on Rails Tutorial 过程中,一直照着书上的例子来,然后到 chapter 6 时,run rspec spec/models/user_spec.rb时出现了如下错误:

Failures:

  1) User when email address is already taken 
     Failure/Error: user_with_same_email.save
     RuntimeError:
       Password digest missing on new record
     # ./spec/models/user_spec.rb:44:in `block (3 levels) in <t
op (required)>'

Finished in 0.09311 seconds
12 examples, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:47 # User when email address i
s already taken 

Randomized with seed 54648

user_spec.rb文件:

require 'spec_helper'

describe User do
    #测试用例之前执行
    before do 
        @user = User.new(
            name: "xautjzd", 
            email: "[email protected]",
            password: "xautjzd",
            password_confirmation: "xautjzd"
        )
    end

    #设置测试用例默认的测试对象
    subject { @user }

    it { should respond_to(:name) }
    it { should respond_to(:email) }
    it { should respond_to(:password_digest) }
    it { should respond_to(:password) }
    it { should respond_to(:password_confirmation) }

    it { should be_valid }

    describe "when name was not present" do
        before { @user.name = " " }
        it { should_not be_valid }
    end

    describe "when email was not present" do
        before { @user.email = " " }
        it { should_not be_valid }
    end

    describe "when name is too long" do
        before { @user.name = 'a' * 51 }
        it { should_not be_valid }
    end

    describe "when email address is already taken" do
        before do
            user_with_same_email = @user.dup
            user_with_same_email.email = @user.email.upcase
            user_with_same_email.save
        end

        it { should_not be_valid }
    end

    describe "when password is not present" do
        before do 
            @user = User.new(
                name: "xautjzd",
                email: "[email protected]",
                password: " ",
                password_confirmation: " "
            )
        end
        it { should_not be_valid }
    end

    describe "when password doesn't match confirmation" do
        before { @user.password_confirmation = "mismatch"}
        it { should_not be_valid }
    end
end

已通过rails g migration add_password_digest_to_users password_digest:string添加此 column,也已经rake db:migrate生成了,貌似是 before 中没有给 password_digest 字段赋值,我添加上就能通过测试,但是教程上并没有给此字段赋值,我参考了其他人学习此教材的例子,也没有赋值,但是都能通过测试。请问这是什么原因呢?

#1 楼 @qhwa Google 已经找了不少答案了,还是没解决。数据库有这个字段,关键是 rspec 测试时,没给这个字段赋值,这点不解。我想正是由于没赋值才导致这个错误的

Gemfile 里面那个 bru… 什么的 gem 有没有?bundle install 没有?model 里加了 has_secure_password 没有?

#3 楼 @blacktulip bcrypt-ruby 已经添加了,然后 bundle install 安装过,model 里面也添加过 has_secure_password。

#4 楼 @xautjzd 我一直不解的是的在 rails console 里面 new 一个 User 时,为何没有给 user_digest 赋值,反而还可以添加呢

#4 楼 @xautjzd 所有代码推上 github 让大家帮你看看吧

#6 楼 @blacktulip 好的,谢谢,我马上推

#8 楼 @xautjzd 知道了,把 app/models/user.rb 里面这行 attr_accessor :password, :password_confirmation 去掉。你用的是 rails 4,attr_accessor 是 rails 3 里面的东西。

#9 楼 @blacktulip 非常感谢,注释掉就没问题了。看来 rails4 与 rails3 还不少差别呢

需要 登录 后方可回复, 如果你还没有账号请 注册新账号