NoMethodError:nil的未定义方法`[]':omniauth gem testing中的NilClass错误

时间:2013-03-04 10:01:28

标签: testing

我使用omniauth-identity gem进行身份验证。首先,我写测试。我因为omniauth-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
这些是我的模特。我使用这样的测试代码     @用户= Fabricate.build(:用户)     @authentication = Authentication.create_with_omniauth(@auth)     发布:创建,@ user

但这给了我错误“NoMethodError:未定义的方法`[]'为nil:NilClass”我该如何解决这个问题?

0 个答案:

没有答案