Help us understand the problem. What is going on with this article?

Railsでテスト時のみRoutingを追加する

More than 3 years have passed since last update.

環境

  • Rails 4.2.6, 5.0.0で確認

方法

ActionDispatch::Routing::RouteSet#drawを使ってRails.application.routesに追加します。
ただし#clear!#finalize!が走ると以前の Routing が消えてしまうので、それを抑制するフラグを切り替える必要があります。

# spec/support/additional_routes.rb
class DummiesController < ActionController::Base
  def show
    render plain: 'hi!'
  end
end

Rails.application.configure do
  routes.disable_clear_and_finalize = true

  routes.draw do
    resource :dummy, only: %i(show)
  end

  routes.disable_clear_and_finalize = false
end

rspec-rails を使っているなら、だいたいは feature spec でのみ必要となると思うので type のメタタグを基準に読み込めれば良いです。
RSpec 3.5 ならこんな感じで必要に応じて読み込ませることができます。

# spec/rails_helper.rb
RSpec.configure do |config|
  config.when_first_matching_example_defined(type: :feature) { require 'support/additional_routes' }
end

参考リンク

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした