Ruby on rails 4 rspec失败,密码为\u

Ruby on rails 4 rspec失败,密码为\u,ruby-on-rails-4,railstutorial.org,Ruby On Rails 4,Railstutorial.org,我正在学习迈克尔·哈特尔的教程。我很难让我的rspec测试正常工作。密码的加密值似乎不一致,从而导致失败。这是令人费解的,因为结果不一致。有时,当我回滚迁移、重新迁移、rake test:prepare并再次运行测试时,测试通过。有时他们仍然失败。这有什么原因吗?或者我是否执行得不正确 下面是用户规范rb的特定代码片段: describe "return value of authenticate method" do before do @user.save e

我正在学习迈克尔·哈特尔的教程。我很难让我的rspec测试正常工作。密码的加密值似乎不一致,从而导致失败。这是令人费解的,因为结果不一致。有时,当我回滚迁移、重新迁移、rake test:prepare并再次运行测试时,测试通过。有时他们仍然失败。这有什么原因吗?或者我是否执行得不正确

下面是用户规范rb的特定代码片段:

describe "return value of authenticate method" do
    before do
        @user.save
    end
    let(:found_user) { User.find_by(email: @user.email) } #found_user should be the user fetched from the database

    describe "with valid password" do
        it { found_user.authenticate(@user.password).should eq @user }
    end

    describe "with invalid password" do
        let(:user_for_invalid_password) { found_user.authenticate("mismatch") }

        it { should_not eq user_for_invalid_password }
        specify { expect(user_for_invalid_password).to be_false }
    end
end
以及我得到的示例错误消息:

$ rspec spec/
.........F...................
Failures:

1) User return value of authenticate method with valid password 
 Failure/Error: it { found_user.authenticate(@user.password).should eq @user }

   expected: #<User id: nil, name: "Example User", email: "user@example.com", created_at: nil, updated_at: nil, password_digest: "$2a$04$ZSegpX1GY1RIcqvDmM4FteBQ.HXzHM2pZtj0mwobetfs...">
        got: #<User id: 1, name: "Example User", email: "user@example.com", created_at: "2014-01-08 09:35:31", updated_at: "2014-01-08 09:35:31", password_digest: "$2a$04$qO1QSOwTEYtioSSmNObKKOgc5K3SQyU4dDk.APqN1egI...">

   (compared using ==)

   Diff:
   @@ -1,2 +1,2 @@
   -#<User id: nil, name: "Example User", email: "user@example.com", created_at: nil, updated_at: nil, password_digest: "$2a$04$ZSegpX1GY1RIcqvDmM4FteBQ.HXzHM2pZtj0mwobetfs...">
   +#<User id: 1, name: "Example User", email: "user@example.com", created_at: "2014-01-08 09:35:31", updated_at: "2014-01-08 09:35:31", password_digest: "$2a$04$qO1QSOwTEYtioSSmNObKKOgc5K3SQyU4dDk.APqN1egI...">

 # ./spec/models/user_spec.rb:82:in `block (4 levels) in <top (required)>'

Finished in 0.38035 seconds
29 examples, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:82 # User return value of authenticate method with valid password 

Randomized with seed 23961

使用rake:reset似乎无法解决问题,测试有时仍然会失败。

什么
rake db:migrate:status显示?

我建议您完全删除数据库,然后尝试迁移。在我看来,在数据库中,已经存储的用户passwd没有被完全删除

rake db:reset
然后跑

rake db:migrate

您应该尝试
subject{@user}
而不是
subject{user}

变量
@user
来自哪里?
rake db:migrate