11
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsのルーティング定義を再読み込みする

Last updated at Posted at 2012-11-12

開発環境でしか現れない特別な機能がある時、それをどうやってテストするか。
環境のフェイクはこうする(Thx hsbt!)。

Rails.stub(:env) { ActiveSupport::StringInquirer.new('development') }

これでRails.env.development?trueになる。
で、config/routes.rbで定義しているルーティングで、開発環境でしか現れない、Rails.env.development?trueの時にしかアクセスできないルーティングがある時、普通にテストを書くと、RAILS_ENVtestなので、死ぬ。URLヘルパーとかで、死ぬ。

if Rails.env.development?
  get  #...
  post #...
end

これはsetupとかbeforeRails.application.reload_routes!をすると解決するみたい。
http://api.rubyonrails.org/classes/ActionDispatch/Routing.html#label-Reloading+routes

before do
  Rails.stub(:env) { ActiveSupport::StringInquirer.new('development') }
  Rails.application.reload_routes!
end
11
12
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?