Created
February 5, 2019 08:09
-
-
Save aileron/d5151dc24171ea924089460c923f03b9 to your computer and use it in GitHub Desktop.
Validate for all routing
This file contains 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 'rails_helper' | |
module ValidateForAllRouting | |
class << self | |
# Admin配下のpath名と、それに合致するcontroller名とaction名を取得 | |
def admin_routes(actions) | |
routes = routes(actions) | |
routes.select { |route| route[:name].include?('admin') } | |
end | |
private | |
def routes(actions) | |
routes = Rails.application.routes.routes | |
routes = routes.map { |route| prepare_route(route) if route.name }.compact | |
routes.select { |route| actions.include?(route[:action]) } | |
end | |
def prepare_route(route) | |
{ name: route.name, action: route.requirements[:action], controller: route.defaults[:controller] } | |
end | |
end | |
end | |
describe 'ValidateForAllRouting' do | |
IGNORE_PATH = [ | |
# 自動テスト対象外のpathを記載 | |
].freeze | |
# 前提データが必要な場合は自動テストを行うのが難しいので、一覧と新規に限定 | |
admin_routes = ValidateForAllRouting.admin_routes(['index', 'new']) | |
# 対象外のpathを除外 | |
target_routes = admin_routes.select { |route| IGNORE_PATH.exclude?(route[:name]) } | |
url_helper = Rails.application.routes.url_helpers | |
# 対象のpathの件数分、遷移しステータスコード200が返ってくることを検証 | |
target_routes.each do |route| | |
describe route[:name] do | |
before do | |
visit url_helper.url_for(host: Capybara.app_host, controller: route[:controller], action: route[:action]) | |
end | |
it "#{route[:controller]} # #{route[:action]}" do | |
expect(page.status_code).to eq 200 | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment