在rails集成测试中使用omniauth模拟谷歌



我有一个示例项目,用户可以使用他们的谷歌帐户登录,现在我想编写一个测试来验证用户是否使用他的谷歌帐户成功登录。但我不知道如何在我的测试文件中验证这件事?这是我现在尝试过的,但它不起作用

  OmniAuth.config.mock_auth[:google] = OmniAuth::AuthHash.new({
                                                                  :provider => 'google',
 :uid => '1337',
 :info => {
 'name' => 'JonnieHallman',
 'email' => 'jon@test.com'
 }
 request.env["devise.mapping"] = Devise.mappings[:user]
 request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:google]

我假设在这之后我的页面内容会发生变化,但它们与之前相同

您可以遵循关于如何使用Omniauth进行集成测试的指南https://github.com/intridea/omniauth/wiki/Integration-Testing

基本上,你的spec/rails_helper.rb 中会有这样的东西

OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:xing] = OmniAuth::AuthHash.new({
  :provider => 'google',
  :uid => '123545',
  :info => {
    :name => "Test",
    :email => "test@test.com"
  },
  :credentials => {
    :token => "token",
    :secret => "secret"
  }
  # etc.
})

然后有一个类似的login_helper

def login
  Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google]
  visit root_path
  click_link 'loginBtn'
end

最新更新