简体   繁体   English

使用 devise_token_auth + omniauth 请求规范

[英]Request spec with devise_token_auth + omniauth

I'm currently writing an API with Rails 5 using devise_token_auth and omniauth as authentication strategy.我目前正在使用devise_token_authomniauth作为身份验证策略使用 Rails 5 编写 API。

To check if the omniauth authentication works, I wrote a request spec:为了检查 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)

When I run the test I have to following error:当我运行测试时,我必须遵循以下错误:

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'

Here is the line 14:这是第 14 行:

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

So I guess that's because omniauth.params is nil .所以我想那是因为omniauth.paramsnil Does it mean that I also have to mock the params?这是否意味着我还必须模拟参数? How?怎么样?

Controller控制器

(ex: Users::OmniauthCallbacksController) (例如:用户::OmniauthCallbacksController)

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

Spec规格

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

or

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM