Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a test-environment route dynamically?

i would like to add a route that is only available in test environment, so i would prefer not to pollute routes.rb file. i cannot seem to find a working way to add a route dynamically after original routes were drawn. i tried this https://gist.github.com/1351762 but that didn't quite work

How can I add a new route after routes.rb has already loaded and processed all the routes?

like image 274
katzmopolitan Avatar asked Oct 17 '25 18:10

katzmopolitan


3 Answers

The with_routing test helper redefines routes within a block.

 with_routing do |map|
   map.draw do
    resources :test, only: [:show]
   end

   get :show
   assert assigns(:test)
 end
like image 75
Matthew Boehlig Avatar answered Oct 20 '25 12:10

Matthew Boehlig


The with_routing test helper redefines routes within a block.

 with_routing do |map|
   map.draw do
    resources :test, only: [:show]
   end

   get :show
   assert assigns(:test)
 end
like image 41
Matthew Boehlig Avatar answered Oct 20 '25 13:10

Matthew Boehlig


After some tries and errors, I found that:

Rails.application.routes.eval_block(Proc.new do                                                                             
  get "/backdoor", :to => "backdoors#backdoor"                                                                         
end)
like image 21
hiroshi Avatar answered Oct 20 '25 12:10

hiroshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!