使用devise_token_auth + omniauth

时间:2016-10-16 08:59:24

标签: ruby-on-rails rspec devise omniauth ruby-on-rails-5

我目前正在使用devise_token_authomniauth作为身份验证策略,使用Rails 5编写API。

要检查omniauth身份验证是否有效,我写了一个请求规范:

OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new(
  {
      provider: 'facebook',
      uid: '123456'
  })
Rails.application.env_config["devise.mapping"] = Devise.mappings[:user]
Rails.application.env_config["omniauth.auth"]  = OmniAuth.config.mock_auth[:facebook]
get '/omniauth/facebook/callback'
expect(response).to have_http_status(:ok)

当我运行测试时,我必须遵循以下错误:

NoMethodError: undefined method `[]' for nil:NilClass
/Users/gaetan/.rvm/gems/ruby-2.3.1/gems/devise_token_auth-0.1.39/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb:14:in `redirect_callbacks'

以下是第14行:

devise_mapping = [request.env['omniauth.params']['namespace_name'],

所以我想这是因为omniauth.paramsnil。这是否意味着我还必须嘲笑参数?怎么样?

1 个答案:

答案 0 :(得分:0)

控制器

(例如:Users :: OmniauthCallbacksController)

def omniauth_params
  request.env['omniauth.params']
end

规范

allow_any_instance_of(Users::OmniauthCallbacksController).to(receive(:omniauth_params).and_return('namespace_name' => 'namespace_name'))

allow_any_instance_of(Users::OmniauthCallbacksController).to(receive(:omniauth_params).and_return(OmniAuth::AuthHash.new({:namespace_name => 'namespace_name'})))