[英]Request spec with devise_token_auth + omniauth
我目前正在使用devise_token_auth
和omniauth
作為身份驗證策略使用 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.params
是nil
。 這是否意味着我還必須模擬參數? 怎么樣?
(例如:用戶::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'})))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.