2

認証にomniauth-identitygemを使用します。まず、テストを書きます。omn​​iauth-identityを使用したテストで行き詰まりました。私のモデルとコントローラーは次のようなものです。

class Authentication
include Mongoid::Document
field :name, type: String
field :uid, type: String
field :provider, type: String

def self.from_omniauth(auth)
find_by_provider_and_uid(auth["provider"], auth["uid"]) || create_with_omniauth(auth)
end

def self.create_with_omniauth(auth)
create! do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["name"]
end
end

def self.find_by_provider_and_uid(provider, uid)
where(provider: provider, uid: uid).first
end
end


class User < Authentication
include Mongoid::Document
include OmniAuth::Identity::Models::Mongoid

field :name, type: String
field :email, type: String
field :password_digest, type: String

validates_presence_of :name
validates_uniqueness_of :email
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
validates_length_of :password, minimum: 2, maximum: 16
end

これらは私のモデルです。このようなテストコードを使用します@user= Fabricate.build(:user)@authentication = Authentication.create_with_omniauth(@auth)post:create、@user

しかし、これによりエラー「NoMethodError:undefined method `[]'for nil:NilClass"」が発生します。これを解決するにはどうすればよいですか?

4

0 に答える 0