Oauth minitest错误,回调链接给出错误

时间:2019-02-05 05:51:17

标签: ruby-on-rails oauth minitest

我正在尝试为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,但我认为我做错了什么。

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

谢谢!

1 个答案:

答案 0 :(得分:0)

我实际上通过清洁一些东西解决了它。

我的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

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

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