[rspec-users] Stub a controllers helper_method in a view template helper spec

Matt Wynne matt at mattwynne.net
Mon Mar 21 07:43:11 EDT 2011


On 21 Mar 2011, at 10:41, Kai Schlamp wrote:

> My ApplicationController exposes a method (e.g. sort_direction) to the
> view templates by using
> helper_method :sort_direction.
> I then use this method in another method (e.g. sort_link) in a view
> helper (application_helper.rb).
> 
> When testing the sort_link method with RSpec (in
> application_helper_spec.rb) I have to stub sort_direction as the test
> seems to run completely independent from the controllers (and thereby
> by to the view templates exposed methods).
> 
> Unfortunately I could not find out how to stub that sort_direction
> method of the controller. I always get "undefined method".
> 
> Here is what I tried so far (inside application_helper_spec.rb):
> 
> helper.stub(:sort_direction)
> controller.stub(:sort_direction)
> view.stub(:sort_direction)
> self.stub(:sort_direction)
> 
> Here the error I get:
> 
> NoMethodError:
>       undefined method `sort_direction' for
> #<RSpec::Core::ExampleGroup::Nested_1

From this error message, it looks like you can just def it inside your describe block.

> 
> There is already some older blog post about that topic (http://
> jakescruggs.blogspot.com/2007/03/mockingstubbing-partials-and-
> helper.html), but it seems that this is not valid anymore for Rails 3
> as there is no @controller.template.
> 
> How do I stub that method?
> 
> Kai
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

cheers,
Matt

matt at mattwynne.net
07974 430184



More information about the rspec-users mailing list