Ruby on rails omniauth facebook与Desive的测试
我首先使用omniauth和Desive用户模型。整合效果很好,但有一个案例失败了。 这是来自omniauth回调控制器的相关代码:Ruby on rails omniauth facebook与Desive的测试,ruby-on-rails,devise,rspec-rails,omniauth-facebook,Ruby On Rails,Devise,Rspec Rails,Omniauth Facebook,我首先使用omniauth和Desive用户模型。整合效果很好,但有一个案例失败了。 这是来自omniauth回调控制器的相关代码: class OmniauthCallbacksController < Devise::OmniauthCallbacksController before_action :find_user, only: [:facebook] def facebook process_omniauth_response('Facebook', reque
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
before_action :find_user, only: [:facebook]
def facebook
process_omniauth_response('Facebook', request.env['omniauth.auth'], @user)
end
def failure
redirect_to root_path, flash: { error: 'Authentication \
failed, please try again!' }
end
private
def find_user
@user = User.from_omniauth(request.env['omniauth.auth'])
end
def process_omniauth_response(provider, env_data, user)
if user.persisted?
sign_in_and_redirect user, event: :authentication
set_flash_message(:notice, :success,
kind: provider) if is_navigational_format?
else
session["devise.#{provider.downcase}_data"] = env_data
redirect_to new_user_registration_url
end
end
end
因此,一切正常,即如果新用户单击Facebook登录,在Facebook验证后,他将使用预填充的数据进入注册页面,但在以下情况下,将在omniauth回调操作后登录页面:
context 'when facebook email doesn\'t exist in the system' do
before(:each) do
stub_env_for_omniauth
get :facebook
end
it { response.should redirect_to new_user_registration_path }
it 'should create authentication with facebook id' do
authentication = User.where(provider: 'facebook', uid: '1234').first
puts authentication.inspect
authentication.should_not be_nil
end
end
以及存根方法:
def stub_env_for_omniauth
request.env['devise.mapping'] = Devise.mappings[:user]
request.env['omniauth.auth'] = OmniAuth::AuthHash.new(
provider: 'facebook', uid: '1234', info: {
email: 'ghost@nobody.com', name: 'mock user'
},
extra: { raw_info: { age: 23 } }
)
end
我得到了这个信息:
预期响应为重定向到,但为重定向到
通过调试,我发现user对象是一个新的,当我在controller的“find_user”方法中检查它时,它不是持久化的,但在spec中,它是一个持久化的
我将感谢任何帮助。
谢谢听起来你有一个调试案例要做。使用Pry,我会在
过程中的if
之前插入一个绑定。Pry
查看发生了什么。我使用byebug,在这里,我发现当我运行rspec时,传递给该方法的用户对象被持久化,但当我正常使用浏览器登录时,它是新的,然后是request.env
,然后问问自己stub\u env*
中进入request.env
的数据是否持续存在。也许请求对象不一样,而且它是存根错误的。
def stub_env_for_omniauth
request.env['devise.mapping'] = Devise.mappings[:user]
request.env['omniauth.auth'] = OmniAuth::AuthHash.new(
provider: 'facebook', uid: '1234', info: {
email: 'ghost@nobody.com', name: 'mock user'
},
extra: { raw_info: { age: 23 } }
)
end