我目前正在使用Rails 5编写一个API,使用devise_token_auth
和omniauth
作为身份验证策略。
为了检查omniauth身份验证是否起作用,我编写了一个请求spec:
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.params
是 nil
。这是否意味着我也需要模拟 params?如何操作?
Rails.application.env_config['omniauth.params'] = { key: 'value' }
但在控制器中返回一个空的哈希值。 - Aref Aslani