開発環境でしか現れない特別な機能がある時、それをどうやってテストするか。
環境のフェイクはこうする(Thx hsbt!)。
Rails.stub(:env) { ActiveSupport::StringInquirer.new('development') }
これでRails.env.development?がtrueになる。
で、config/routes.rbで定義しているルーティングで、開発環境でしか現れない、Rails.env.development?がtrueの時にしかアクセスできないルーティングがある時、普通にテストを書くと、RAILS_ENVがtestなので、死ぬ。URLヘルパーとかで、死ぬ。
if Rails.env.development?
get #...
post #...
end
これはsetupとかbeforeでRails.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