首页 > 解决方案 > Omniauth 模拟在 rails rspec 中不起作用。谷歌登录按钮没有被点击

问题描述

在遵循 Google Login in Rails 的集成测试互联网教程之后。我来到这里。但它不起作用。

# frozen_string_literal: true

require "rails_helper"

def stub_omniauth
    OmniAuth.config.test_mode = true
    OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new({
        provider: "google_oauth2",
        uid: "12345678910",
        info: {
            email: "limsammy1@gmail.com",
            first_name: "Sam",
            last_name: "Lim"
        },
        credentials: {
            token: "abcdefg12345",
            refresh_token: "abcdefg12345",
            expires_at: DateTime.now,
        }
    })
end

RSpec.feature "user logs in" do
    scenario "using google oauth2" do
        visit login_path
        stub_omniauth
        Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google_oauth2]
        expect(page).to have_css("a.google-login-button")
        find("a.google-login-button").click
        expect(page.current_path).to eq(dashboard_path)
    end
end

我假设在运行测试后我的页面内容会改变,但它们和以前一样。

标签: ruby-on-railsrspeccapybaraomniauth

解决方案


推荐阅读