- Rails 5.0.0.beta3
require_to_signin
というbefore_action
のテストをしたい- その
before_action
はApplicationController
に定義されている - 実際にその
before_action
が呼ばれるのは、ApplicationController
を継承した各コントローラー内だが、その全てで確認するのは嫌なので、動作はApplicationController
に対するテストとして確認したい - 各コントローラーのアクションでは
before_action
でrequire_to_signin
が呼ばれていることだけ確認したい
Created
April 5, 2016 07:15
-
-
Save 5t111111/437ffe367bf15a921dddd8e9c7a9d2c7 to your computer and use it in GitHub Desktop.
ApplicationController で定義されている before_action の動作テストと、それが実際に呼ばれていることのテストについて
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
# clas_eval でダミーのアクションを定義する | |
ApplicationController.class_eval do | |
def dummy_action | |
render :nothing | |
end | |
end | |
# Rails.application.routes.draw が呼ばれたときにルートをクリアする動作を無効にする | |
Rails.application.routes.disable_clear_and_finalize = true | |
# ダミーのアクション用のルートを定義する | |
Rails.application.routes.draw do | |
get 'dummy_action' => 'application#dummy_action' | |
end | |
class ApplicationControllerTest < ActionDispatch::IntegrationTest | |
test 'require_to_signin should redirect someone to sign in page without signing in' do | |
ApplicationController.stub_any_instance(:current_user, nil) do | |
get dummy_action_url | |
assert_equal 'サインインしてください。', flash[:alert] | |
assert_redirected_to signin_path | |
end | |
end | |
end | |
# もし他の before_action をスキップし、 | |
# 純粋に対象となる before_action だけテストしたい場合は、 | |
# skip_before_filter を使うか before_action のメソッドを stub すればいいと思う |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class VotesControllerTest < ActionDispatch::IntegrationTest | |
test 'require_to_signin is invoked before any actions' do | |
# テスト確認用の一時的なキーを用意 | |
require 'securerandom' | |
random_key = SecureRandom.hex | |
# テスト対象の before_action と差し変えるためのダミーの lambda を用意 | |
# redirect_to を使ってどこかに飛ばし callabck chain が途切れるようにする | |
dummy_proc = -> do | |
flash[random_key] = random_key | |
redirect_to root_url | |
end | |
# before_action を用意した lambda で stub して実行 | |
ApplicationController.stub_any_instance(:require_to_signin, dummy_proc) do | |
%w(total new).each do |action| | |
get url_for(controller: 'votes', action: action) | |
assert_equal random_key, flash[random_key] | |
end | |
%w(edit show).each do |action| | |
get url_for(controller: 'votes', action: action, id: 1) | |
assert_equal random_key, flash[random_key] | |
end | |
post url_for(controller: 'votes', action: 'create') | |
assert_equal random_key, flash[random_key] | |
patch url_for(controller: 'votes', action: 'update', id: 1) | |
assert_equal random_key, flash[random_key] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment