Oauth 最小测试错误,回调链接给出错误



我正在尝试为 OmniAuth 用户编写测试,在设置test_helper后,我遇到了一个错误的 URI 错误。分享以下详细信息:

test_helper.rb

# OmniAuth auth mock for testing
  def setup_omniauth_mock (user)
    OmniAuth.config.test_mode = true
    OmniAuth::AuthHash.new ({
    'provider'            => 'google',
    'uid'                 => '123545',
    'user_id'             => '2',
    'first_name'          => 'X',
    'last_name'           => 'XYZ',
    'email'               => 'xxyz@example.com',
    'image'               => 'https://lh3.googleusercontent.com//photo.jpg',
    'oauth_token'         => 'abcdef12345',
    'oauth_expires_at'    => DateTime.now,
    })
    OmniAuth.config.add_mock(:google, OmniAuth::AuthHash.new)
    get '/auth/":google"/callback'
    Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google]
    get '/auth/:google/callback'
  end 

我得到的错误:

    test_validating_a_Google_OAuth_user#SessionsControllerTest (0.49s)
URI::InvalidURIError:         URI::InvalidURIError: bad
URI(is not URI?): http://www.example.com:80/auth/":google"/callback
test/test_helper.rb:42:in `setup_omniauth_mock'

现在我按照这里的文档[Oauth集成测试][1]

[1]:https://github.com/omniauth/omniauth/wiki/Integration-Testing 但我认为我做错了什么。

有人可以帮我指导一下吗?

谢谢!J.

我实际上通过清理一下东西来解决它。

我现在的test_helper.rb:

 # OmniAuth auth mock setup for testing
  setup do
    OmniAuth.config.test_mode = true
    Rails.application.env_config["omniauth.auth"] =
    OmniAuth.config.mock_auth[:google]
  end
  #teardown OmniAuth mock setup
  teardown do
    OmniAuth.config.test_mode = false
  end
  #Google OAuth mock
  def google_oauth2_mock (user)
    OmniAuth.config.mock_auth[:google]
    OmniAuth::AuthHash.new ({
    'provider'            => 'google_oauth2',
    'uid'                 => '123545',
    'user_id'             => '2',
    'first_name'          => 'X',
    'last_name'           => 'XXYZ',
    'email'               => 'xxyzjam@example.com',
    'image'               => 'https://lh3.googleusercontent.com/photo.jpg',
    'oauth_token'         => 'abcdef12345',
    'refresh_token'       => '12345abcdef',
    'oauth_expires_at'    => DateTime.now,
    })
  end

我将路由放在各个测试中,这使我能够顺利运行测试套件。

希望我能为您节省一些时间和挫折。

最新更新