0

モデル仕様 (user_spec.rb) で何が間違っているのかわかりません。認証方法が有効なパスワードで機能することを確認しようとしていますがUser.find_by、仕様の一部で問題が発生していると思いますnil. :authenticate、:password、および :password_confirmation に応答することを確認しました (以下のコードでは、読みやすくするために省略しています)。

仕様の実行時に発生する失敗は次のとおりです。

Failures:

  1) User return value of authenticate method with valid password should eq #<User id: 136, fullname: "Sherwood Hickle", email: "jewell.brekke@stokes.net", created_at: "2013-11-06 23:04:35", updated_at: "2013-11-06 23:04:35", password_digest: "$2a$04$O5V2X9sZrl/u2T9W25c3Pu/PU6XaIvtZSIB39Efkid6a...">
     Failure/Error: it { should eq found_user.authenticate(user.password) }

       expected: #<User id: 136, fullname: "Sherwood Hickle", email: "jewell.brekke@stokes.net", created_at: "2013-11-06 23:04:35", updated_at: "2013-11-06 23:04:35", password_digest: "$2a$04$O5V2X9sZrl/u2T9W25c3Pu/PU6XaIvtZSIB39Efkid6a...">
            got: #<User id: nil, fullname: nil, email: nil, created_at: nil, updated_at: nil, password_digest: nil>

       (compared using ==)

       Diff:
       @@ -1,2 +1,2 @@
       -#<User id: 136, fullname: "Sherwood Hickle", email: "jewell.brekke@stokes.net", created_at: "2013-11-06 23:04:35", updated_at: "2013-11-06 23:04:35", password_digest: "$2a$04$O5V2X9sZrl/u2T9W25c3Pu/PU6XaIvtZSIB39Efkid6a...">
       +#<User id: nil, fullname: nil, email: nil, created_at: nil, updated_at: nil, password_digest: nil>

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

user_spec.rb:

require 'spec_helper'

describe User do

describe "return value of authenticate method" do
  user = FactoryGirl.create(:user)
  let(:found_user) { User.find_by(email: user.email) }

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

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

    it { should_not eq user_for_invalid_password }
    specify { expect(user_for_invalid_password).to be_false }
  end
end

end

また、FactoryGirl を使用してユーザー名と電子メール アドレスを生成していますが、失敗とは何の関係もないと思います。

require 'faker' 

FactoryGirl.define do 
  factory :user do |u| 
    u.fullname { Faker::Name.name } 
    u.email { Faker::Internet.email }
    u.password { "foobar" }
    u.password_confirmation { "foobar" }
  end 
end
4

1 に答える 1