[rspec-users] RSpec 2 view example: render_template

David Chelimsky dchelimsky at gmail.com
Sun Jun 20 11:16:31 EDT 2010

On Jun 20, 2010, at 9:35 AM, Michael Schuerig wrote:

> On Sunday 20 June 2010, David Chelimsky wrote:
>> On Jun 20, 2010, at 7:30 AM, Michael Schuerig wrote:
>>> On http://github.com/rspec/rspec-rails under "View specs" there's
>>> an example
>>> describe "events/index.html.erb" do
>>> it "renders _event partial for each event" do
>>>   assign(:events, [stub_model(Event), stub_model(Event)])
>>>   render
>>>   view.should render_template(:partial => "_event", :count => 2)
>>> end
>>> end
>>> Is this indeed correct? From the outset, it looks wrong because it
>>> sets an expectation (view.should ...) after the call to render.
>>> Then, no matter if I leave that line where it is or put it before
>>> the render, I get an exception
>>> undefined method `render_template' for
>>> #<RSpec::Core::ExampleGroup::Nested_1:0x7f6c0632fea0>
>> The README you're looking at is stored in git with git HEAD, and
>> aligns with that code. This feature was added after the last
>> release, and works as advertised if you're working with git HEAD,
>> and will be part of the next release.
> Oops, you're completely right, of course.
>> If you're using an earlier release, then you should follow the README
>> for that release:
>> http://github.com/rspec/rspec-rails/tree/v2.0.0.beta.12
> That README doesn't say anything on the topic. This probably means that 
> I have to wait for the next beta. I already found out, that I can set 
> expectations with view.should_receive(:render) like I did in pre-2.0 
> with template.should_receive(:render).

That doesn't actually work like it did in 1.x, because the rails internals have changed:

  it "renders the edit gadget form" do
    view.should_receive(:render).with(:partial => "_form")

$ rspec spec/views/gadgets/edit.html.erb_spec.rb 

1) gadgets/edit.html.erb renders the edit gadget form
    Failure/Error: render
    #<ActionView::Base:0x103696f28> received :render with unexpected arguments
      expected: ({:partial=>"_form"})
           got: ({:template=>"gadgets/edit.html.erb"}, {})

You have to say this instead:

  it "renders the edit gadget form" do
    view.should_receive(:_render_partial).with(hash_including(:partial => "form"))

... which is a bit of a kludge and subject to changes in rails' internals. That's why I want to use "render_template", which delegates to "assert_template", which is a rails API and less likely to break.

Make sense?

> Michael
> -- 
> Michael Schuerig
> mailto:michael at schuerig.de
> http://www.schuerig.de/michael/
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list