Forum: RSpec Confusion At Failing Specs

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5030981121b21bed8aee074f68bd5074?d=identicon&s=25 RSL ___ (rsl)
on 2008-11-07 22:48
(Received via mailing list)
Can someone please explain to me why this spec fails

it "should work" do
  template.stub!(:render)
  template.should_receive(:render).with(:partial => "foo")
  render @template_with_render_partial_foo
end

but this spec passes

it "should work" do
  @foo.stub!(:bar)
  @foo.should_receive(:bar).with(:baz)
  @foo.bar :baz
end

It seems like they should work similarly and indeed this has worked for
me
until I switched to gem rspec-rails from vendor/plugin [recent]. Not
sure
what I'm doing wrong here.
F86901feca747abbb5c6c020362ef2e7?d=identicon&s=25 Zach Dennis (zdennis)
on 2008-11-07 23:11
(Received via mailing list)
On Fri, Nov 7, 2008 at 4:35 PM, Russell Norris <rsl@swimcommunity.org>
wrote:
> end
>
> end
>
> It seems like they should work similarly and indeed this has worked for me
> until I switched to gem rspec-rails from vendor/plugin [recent]. Not sure
> what I'm doing wrong here.
>

In the first example, you call "stub!(:render)". This is stubbing all
calls to render with any arguments. So you've stubbed the call that
"render @template_with_render_partial_foo" makes.

Try: template.stub!(:render).with(:partial => anything)


--
Zach Dennis
http://www.continuousthinking.com
http://www.mutuallyhuman.com
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2008-11-07 23:28
(Received via mailing list)
On Fri, Nov 7, 2008 at 4:35 PM, Russell Norris <rsl@swimcommunity.org>
wrote:
> Can someone please explain to me why this spec fails
>
> it "should work" do
>
>   template.stub!(:render)
>
>   template.should_receive(:render).with(:partial => "foo")
>
>   render @template_with_render_partial_foo

Can you put the rest of the spec and the view in? What failure message
are you getting?
5030981121b21bed8aee074f68bd5074?d=identicon&s=25 RSL ___ (rsl)
on 2008-11-08 00:16
(Received via mailing list)
Zach,

Thank you so much. That works for me.

I'm still confused why stubbing all calls to :bar on @foo would allow
@foo.should_receive(:bar).with(:baz) when this doesn't work though. But
I
feel like I'm looking a gift horse in the mouth here!

RSL
5030981121b21bed8aee074f68bd5074?d=identicon&s=25 RSL ___ (rsl)
on 2008-11-08 00:20
(Received via mailing list)
Per David's request i made a pastie of the whole view and the whole test
of
an example of this. as well as the output. http://pastie.org/309868

RSL
F86901feca747abbb5c6c020362ef2e7?d=identicon&s=25 Zach Dennis (zdennis)
on 2008-11-08 02:35
(Received via mailing list)
On Fri, Nov 7, 2008 at 5:17 PM, Russell Norris <rsl@swimcommunity.org>
wrote:
> Zach,
>
> Thank you so much. That works for me.
>
> I'm still confused why stubbing all calls to :bar on @foo would allow
> @foo.should_receive(:bar).with(:baz) when this doesn't work though. But I
> feel like I'm looking a gift horse in the mouth here!

The reason this doesn't work is because rspec's "render" method in
your view example is just wrapping a call to the actual template.
Here's what I mean. Given your example:

  it "should work" do
    template.stub!(:render)
    template.should_receive(:render).with(:partial => "foo")
    render @template_with_render_partial_foo
    # in your view you call <%= render :partial => "foo" %>
  end

Here's what it is really doing:

  it "should work" do
    @controller.template.stub!(:render)
    @controller.template.should_receive(:render).with(:partial => "foo")
    @controller.template.render @template_with_render_partial_foo
    # in your view you call <%= render :partial => "foo" %>
    @controller.template.render :partial => "foo"
  end

The @controller.template is the actual template that gets rendered.
The "render" call in your example is made on the same template object
that the actual "render :partial" call is made on. So, when you make
the call to render the @template_with_render_partial_foo you have
stubbed out that render method and it never actually renders anything
causing your  example to fail because it never gets to the view
template that calls 'render :partial => "foo"'

Hopefully this helps clear it up?

--
Zach Dennis
http://www.continuousthinking.com
http://www.mutuallyhuman.com
This topic is locked and can not be replied to.