DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Firing A Controller's Action From The Console

08.25.2005
| 27356 views |
  • submit to reddit
        It seems like a simple task, but here's how you can simulate the calling of a controller's action:

ruby script/console

irb> require 'action_controller/test_process'
irb> require 'application'
irb> require 'site_controller'
irb> request = ActionController::TestRequest.new
irb> response = ActionController::TestResponse.new
irb> request.env['REQUEST_METHOD'] = 'GET'
irb> request.action = "late_employee"
irb> InfoController.process(request,response)

Basically, it's like getting inside of a TestUnit method, but you have to do the dirty work yourself.    

Comments

Ruslan Voloshin replied on Fri, 2007/06/22 - 3:06am

app.get '/info/late_employee' response = app.response response.body This helped me thanks

Snippets Manager replied on Thu, 2008/12/11 - 11:10am

try: app.get '/info/late_employee' response = app.response

Snippets Manager replied on Sat, 2007/09/15 - 5:15pm

For something simple like this you can also use (from ./script/console): app.get '/info/late_employee' But I haven't been able to get at the response object with that yet, so this was helpful today. Thanks!

Snippets Manager replied on Mon, 2012/05/07 - 2:14pm

Be careful, requiring action_controller/test_process will cause the test suite to attempt to run on exit. It is better to stuff $" with the correct files to prevent test/unit from firing.

Snippets Manager replied on Mon, 2012/05/07 - 2:14pm

You can use the load command on a .rb that has all but the last two lines on it! Thanks Duane, I was wondering how to do that. Good console post from Robby here: http://www.robbyonrails.com/articles/2005/08/18/are-you-a-console-master

Snippets Manager replied on Mon, 2012/05/07 - 2:14pm

Yikes! That's cool, but a lot of typing :)