Land rush

I have a workplace running gag with a friend at a client wherein when we encounter a Rails misfeature (especially the ones that have been solved carefully and elegantly in Java or elsewhere), we rip on Rails mercilessly to make ourselves feel better. My friend came up with this one almost unprovoked, I thought it eminently bloggable:

"It's like the land rush. We are all so excited about the free land that we forgot about all the bears and Indians waiting to kill us."

How to stub out Rails helper methods with Mocha?

UPDATE The code given doesn't actually work. I'd love to hear if anyone knows of code that will do this. I hacked around the problem a very ugly way.

Rails helper methods are defined in modules and mixed into objects...of what class? Turns out it's anonymous copies of ActionView::Base. How does one mock or stub methods on such a beast? Here's a solution I found:


class MyController
def rescue_action(e) raise e end
attr_reader :template
end

class MyControllerTest < Test::Unit::TestCase
def setup
@controller = ...
end

def test_should_stub_helper_method
@controller.template.expects(:random_helper_method).returns('')
...
end
end

Thanks to Matthew Dieter for posting about this. I had to add the attr_reader line to get this to work.