[rspec-users] Specs for Helpers that call render

David Chelimsky dchelimsky at gmail.com
Sat Nov 3 01:46:38 EDT 2007


On Nov 2, 2007 8:32 PM, Peter <peter at netsprout.com> wrote:
> The helper spec I am writing tests a helper method that calls render.
>
> ##
> module HelperHelper
>    def render_partial
>      render :partial => 'partial'
>    end
> end
> ##
>
> The helper spec.
>
> ##
> describe HelperHelper do
>    it "should render partial" do
>      render_partial.should_not == nil
>    end
> end
> ##
>
> The output generated
>
> ##
> $ spec spec/helpers/home_helper_spec.rb
> .F
>
> 1)
> NoMethodError in 'HelperHelper should render partial'
> You have a nil object when you didn't expect it!
> The error occurred while evaluating nil.render
> app/helpers/helper_helper.rb:4:in `render_partial'
> ./spec/helpers/helper_helper_spec.rb:3:
>
> Finished in 0.00000 seconds
>
> 1 example, 1 failure
> ##
>
> I tried adding a before(:each) to setup response, controller, and
> template objects that render would be called on but the error was
> always the same.  I am still uncertain I did this correctly although
> I did spend some time looking at the render definition in the rails
> source.
>
> Is there a standard way of setting up specs for helpers that call
> render?

Helper specs create an object to run in and include a standard set of
helpers and the helper you are spec'ing. There is no access to
services that come from the controllers or views the helper gets mixed
into when the app is running.

I would just set a message expectation on self, like this:

describe HelperHelper do
  it "should render partial" do
    self.should_receive(:render).with(:partial => 'partial')
    render_partial
  end
end

HTH,
David


More information about the rspec-users mailing list