views:

467

answers:

3

How would I quickly debug helper methods in script\console. I'm talking about making changes and then debugging, over and over again.

This is a lot easier with Model methods, since all I have to do is use reload! to test the updated code, whereas to test a helper method, I have to do something like this

foo = ActionView::Base.new foo.extend YourHelperModule

each time to I want to test a change.

What does reload! do? and can I modify it to add the above lines of code?

+2  A: 

I would suggest not using script console and writing tests in either Test::Unit or rspec instead. Google should get you pointed in the right direction there is a ton of information out there.

railsninja
+3  A: 

I don't think you can do that without hacking Rails. However, there's a workaround - debugging helper method in rails debugger:

1) gem install ruby-debug

2) ruby script/server --debugger

3) place <% debugger %> into some view and open that page in browser

4) server window "turns into" console, where you can debug helper methods

5) 'return' command ends the debugging

If you modify the helper method and run the debugger again, you will get recent version of the method.

More info about debugger is here: http://railscasts.com/episodes/54-debugging-with-ruby-debug

Lukas Stejskal
A: 

If you're doing something "again and again" then you should be automating it. Assuming you know what your helper function should do then as mentioned elsewhere you should be able to write a test (or tests) for it.

Here's a sample that tests application_helper. It lives in my test/unit directory:

require 'test_helper'

class ApplicationHelperTest < ActiveSupport::TestCase

  include ApplicationHelper

  test "number_as_pct shows 2dp as default" do
    assert_equal "1.10%", number_as_pct(0.011)
  end
  test "number_as_pct shows more dp when required" do
    assert_equal "1.1000%", number_as_pct(0.011, :precision => 4)
  end
end
Mike Woodhouse