2

我使用omniauth-identity gem 进行身份验证。首先,我编写测试。我被困在全能身份的测试中。我的模型和控制器是这样的:

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