やること
- RSpec実行時のGoogle認証をテストモードに設定
- スタブユーザーのデータを設定
RSpec実行時のGoogle認証をテストモードに設定
rails_helper.rbに以下の設定を追加
spec/rails_helper.rb
OmniAuth.configure do |c|
c.test_mode = true
end
スタブユーザーのデータを設定
先ほどrails_helper.rbに追記したOmniAuth.configureに追加
spec/rails_helper.rb
OmniAuth.configure do |c|
c.test_mode = true
c.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new({
provider: "google_oauth2",
uid: "111111111111111111111"
})
end
テストを書く
Google認証ボタンをクリックする動作を入れると勝手に設定したユーザーでログインしてくれます。
app/views/before_login.html.erb
<h1>Pages#before_login</h1>
<p>Find me in app/views/pages/before_login.html.erb</p>
<% unless user_signed_in? %>
<%= link_to user_google_oauth2_omniauth_authorize_path do %>
<%= image_tag "btn_google_signin_dark_normal_web.png", id: "google_signin_button" %>
<% end %>
<% end %>
spec/features/login_spec.rb
require "rails_helper"
feature "login" do
scenario "Google認証できること" do
visit root_path
find("#google_signin_button").click
sleep 1
expect(current_path).to eq auths_after_login_path
end
end
認証処理完了前にパスのチェックが実行されてしまうようでFailureになってしまうのでfindとexpectの間にsleep 1を入れてます。