Devise 1.5 + Omniauth 1.0 + Facebook:未定义的方法`extra` [英] Devise 1.5 + Omniauth 1.0 + Facebook: undefined method `extra`

查看:161
本文介绍了Devise 1.5 + Omniauth 1.0 + Facebook:未定义的方法`extra`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的黄瓜测试与Devise 1.5和Omniauth 1.0一起使用Facebook认证。有趣的是,它适用于开发模式,但是当运行Cukes测试时,它会失败,并显示以下消息:

I'm trying to get my Cucumber test to work with Devise 1.5 and Omniauth 1.0, with Facebook authentication. Funny thing is, it works on development mode, but when run the Cukes test, it fails with this message:

undefined method `extra' for #<Hash:0x007f95f0d26260> (NoMethodError)
./app/models/user.rb:13:in `find_for_facebook_oauth'
./app/controllers/users/omniauth_callbacks_controller.rb:4:in `facebook'
(eval):2:in `click_link'
./features/step_definitions/web_steps.rb:58:in `/^(?:|I )follow "([^"]*)"$/'
features/facebook_auth.feature:11:in `When I follow "Sign in with Facebook"'

以下是相应的方法:

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)

    data = access_token.extra.raw_info
    if user = User.where(:email => data.email).first
        user
    else 
        User.create!(:email => data.email, :password => Devise.friendly_token[0,20]) 
    end
end

为了让Cukes测试全部是绿色的,我必须做这个解决方法,然后打破开发模式代码,所以现在我这样做:

To get the Cukes test to be all green, I had to do this workaround, which then breaks the Development mode code. So for now, I'm doing this:

    case Rails.env
    when "test"
        data = access_token['extra']['user_hash']
        if user = User.find_by_email(data["email"])
            user
        else 
            User.create!(:email => data["email"], :password => Devise.friendly_token[0,20])
        end
    else
        data = access_token.extra.raw_info
        if user = User.where(:email => data.email).first
            user
        else 
            User.create!(:email => data.email, :password => Devise.friendly_token[0,20]) 
        end
    end     

似乎违规行是 data = access_token.extra .raw_info

我嘲笑Facebook哈希的方式是:

The way I'm mocking the Facebook hash is:

OmniAuth.config.add_mock(:facebook, {
    :uid => '12345',
    :nickname => 'zapnap',
    :extra => {
      :user_hash => {
        'email' => 'someone@webs.com'
      }
    }
  })

我已经通过在最后一行添加了 OmniAuth.config.test_mode = true test.rb

And I have turned on OmniAuth.config.test_mode = true by appending it at the last line of test.rb.

任何想法都将不胜感激!

Any ideas would be greatly appreciated!

推荐答案

我有完全相同的错误,可以以某种方式让它变成全部绿色。
虽然我使用存根而不是模拟
如这里写的,希望它可以请帮助。

I got exactly the same error and could somehow get it to be all green. Although I use stub instead of mock as is written here, hope it can be a help.

正如讨论的那样这里,这是因为OmniAuth1.0使用Hashie :: Mash的部分我们有错误。
所以,我们发现使用Hashie :: Mash对象而不是Hash。
为了这样做,

As it is discussed here, this occurs because OmniAuth1.0 uses Hashie::Mash for the part we got error. So, we figured out to use a Hashie::Mash object to return instead of a Hash. In order to do so,

我把它添加到spec_helper ..

I added this to spec_helper..

require "omniauth"

和这样修改的存根方法.. >

and modified stub method like this..

def stub_env_for_omniauth
  request.env["devise.mapping"] = Devise.mappings[:user]
  pre = { "omniauth.auth" => { "provider" => "facebook", "uid" => "1234", "credentials" => {"token" => "abcdefg"}, "extra"=>{"raw_info" => {"id" => "1234567", "email" => "ghost@nobody.com", "name" => "Mark", "gender" => "male" }}}}
  env = OmniAuth::AuthHash.new(pre)
  @controller.stub!(:env).and_return(env)
end

这适用于我。

这篇关于Devise 1.5 + Omniauth 1.0 + Facebook:未定义的方法`extra`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆