Firing Rails Controller Actions from Command line (console) 4

Posted by science on July 24, 2007

Kanji command symbol

I’ve been looking into firing off events from the console (ruby script/console). It turns out to be easy - there’s a very nice write up of the basics here. One thing that was missing was how to get access to the response results from the command. I’ve got a method that’s working that I’ll share here.
Let’s say you’ve got a controller named “SampleController” with an action that looks (for simplicity) like:

def echo
  render(:text => 'echo!')
end

You can fire up the console with “ruby script/console” from your root Rails app folder. From this command line type this:

app.get('sample/echo')
output = app.html_document.root.to_s

Now the variable “output” holds a string representation of the html “payload” that would be delivered over html to a user’s browser. Kind of handy! Anyone with a better way to get at the html payload - please post comments here.

If you wanted to put this sort of thing into a rake task - you’d set it up as follows.. (First add to the top of your rake task the line:

require 'action_controller/integration'

Then add this code wherever is convenient:


namespace :actions do
desc "test action"
  task "echo" do
    app = ActionController::Integration::Session.new;
    app.get('/sample/echo')
    puts app.html_document.root.to_s
  end
end

Then you could fire this off with the command (from your Rails app folder):

rake actions:echo

I’ve been getting weird testing output when I run this from the command line -presumably related to the Integration::Session’s testing heritage? Anyone else getting this kind of spurious output? It’s more annoying than problematic:

Started

Finished in 0.0 seconds.

0 tests, 0 assertions, 0 failures, 0 errors
Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. science Tue, 09 Oct 2007 15:25:57 EDT

    Also noticed recently that if you want to get access to the html body of an action in a test environment (a functional test, that is), the code you could use is:

    html_response = @response.body

  2. olddog Wed, 28 Nov 2007 11:46:30 EST

    GRREAT post , exactly what I needed.

  3. science Tue, 11 Dec 2007 23:16:08 EST

    Word to the wise: you may get weird errors if you use this line when running integration tests (i.e. when RAILS_ENV != ‘production’)

    
    require 'action_controller/integration'
    

    So it’s better (though fugly) to use something like:

    
    require 'action_controller/integration' unless (RAILS_ENV != 'production')
    

    Basically, from what I can tell, Rails is re-entrantly calling development mode and then test mode, for purposes of parsing rb files. So if you prevent this require statement from being called in dev or test mode, then you don’t get the collision with action_controller/integration. If I knew enough about this part of Rails, I’d submit a patch - anyone with ideas - post them here!

  4. science Sat, 05 Apr 2008 22:49:36 EDT

    An interesting alternative to this technique is blogged here: http://snippets.dzone.com/posts/show/600

Comments