我目前正在使用 Rails 5 编写一个 API,使用 devise_token_auth
和 omniauth
作为身份验证策略。
为了检查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.params
是 nil
。这是否意味着我还必须模拟参数?怎么办?
最佳答案
Controller
(例如: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'})))
关于ruby-on-rails - 使用 devise_token_auth +omniauth 请求规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40068599/