Matt Darby
2010-Nov-16 14:16 UTC
[rspec-users] view.should render_template best practices?
I''ve been looking for the definitive answer for months now, and the RSpec book doesn''t touch on it at all: How do we now handle stubbing out rendering of partials in view specs in RSpec2? I have a large (35K+ lines of views and related specs) that I''m trying to upgrade to Rails3/RSpec2. My views use partials pretty extensively and this issue is a huge blocker for me. before do view.should render_template("event_list", :locals => {:calendar => @calendar}) end causes all my related specs fail with: expecting <"event_list"> but rendering with <"">. Expected block to return true value. Any advice?
Evgeniy Dolzhenko
2010-Nov-17 07:01 UTC
[rspec-users] view.should render_template best practices?
render_template matcher is to be used after the view is rendered. In your case something like view.stub!(:render).with(:partial => "event_list", :locals => {:calendar => @calendar}) should do the trick On Tuesday, November 16, 2010, Matt Darby <mdarby at dynamix-ltd.com> wrote:> I''ve been looking for the definitive answer for months now, and the > RSpec book doesn''t touch on it at all: > > How do we now handle stubbing out rendering of partials in view specs > in RSpec2? > > I have a large (35K+ lines of views and related specs) that I''m trying > to upgrade to Rails3/RSpec2. My views use partials pretty extensively > and this issue is a huge blocker for me. > > > before do > ?view.should render_template("event_list", :locals => {:calendar => > @calendar}) > end > > causes all my related specs fail with: > > expecting <"event_list"> but rendering with <"">. > Expected block to return true value. > > > Any advice? > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Evgeniy Dolzhenko
2010-Nov-17 08:15 UTC
[rspec-users] view.should render_template best practices?
I''m sorry for introducing the confusion, but `view.stub!(:render)` won''t work, I should''ve read the list more attention http://groups.google.com/group/rspec/browse_thread/thread/1590942f2827f55 On Wed, Nov 17, 2010 at 10:01 AM, Evgeniy Dolzhenko <dolzenko at gmail.com> wrote:> render_template matcher is to be used after the view is rendered. > In your case something like > ?view.stub!(:render).with(:partial => "event_list", :locals => > {:calendar => @calendar}) > ?should do the trick > > On Tuesday, November 16, 2010, Matt Darby <mdarby at dynamix-ltd.com> wrote: >> I''ve been looking for the definitive answer for months now, and the >> RSpec book doesn''t touch on it at all: >> >> How do we now handle stubbing out rendering of partials in view specs >> in RSpec2? >> >> I have a large (35K+ lines of views and related specs) that I''m trying >> to upgrade to Rails3/RSpec2. My views use partials pretty extensively >> and this issue is a huge blocker for me. >> >> >> before do >> ??view.should render_template("event_list", :locals => {:calendar => >> @calendar}) >> end >> >> causes all my related specs fail with: >> >> expecting <"event_list"> but rendering with <"">. >> Expected block to return true value. >> >> >> Any advice? >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> >
David Chelimsky
2010-Nov-17 11:37 UTC
[rspec-users] view.should render_template best practices?
On Nov 16, 2010, at 8:16 AM, Matt Darby wrote:> I''ve been looking for the definitive answer for months now, and the > RSpec book doesn''t touch on it at all: > > How do we now handle stubbing out rendering of partials in view specs > in RSpec2? > > I have a large (35K+ lines of views and related specs) that I''m trying > to upgrade to Rails3/RSpec2. My views use partials pretty extensively > and this issue is a huge blocker for me. > > before do > view.should render_template("event_list", :locals => {:calendar => > @calendar}) > end > > causes all my related specs fail with: > > expecting <"event_list"> but rendering with <"">. > Expected block to return true value. > > > Any advice?In rspec-2/rails-3, view.should render_template("event_list") is no longer a message expectation (pre-action), but rather delegates to Rails'' assert_template method, so it needs to come _after_ the action. It should work exactly as you have it above, just after the action. This is documented in the rspec-rails README (https://github.com/rspec/rspec-rails - see sections on View Specs and Matchers), though not with an upgrade note pointing out that it changed. I''ll make that addition shortly. As for stubbing the template, so that it is not actually rendered, RSpec doesn''t offer a way to do this in view specs and I don''t think Rails does either. I added an issue for this (https://github.com/rspec/rspec-rails/issues/issue/263), and hopefully we''ll be able to resolve it soon. In the mean time, the examples will need to supply any data needed by the template being spec''d _and_ any of the partials. Side note: I urge you not to put message expectations (pre "When") or state-based expectations (post "When") in before/after blocks. It makes examples difficult to understand, and results in specifying the same things in every example in a group. Rather, have one example that specifies each individual thing. In this case: it "renders the event_list with @calendar" do render view.should render_template( "event_list", :locals => {:calendar => @calendar} ) end HTH, David -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101117/964b03d8/attachment.html>
David Chelimsky
2010-Nov-17 14:07 UTC
[rspec-users] view.should render_template best practices?
On Nov 17, 2010, at 5:37 AM, David Chelimsky wrote:> On Nov 16, 2010, at 8:16 AM, Matt Darby wrote: > >> I''ve been looking for the definitive answer for months now, and the >> RSpec book doesn''t touch on it at all: >> >> How do we now handle stubbing out rendering of partials in view specs >> in RSpec2? >> >> I have a large (35K+ lines of views and related specs) that I''m trying >> to upgrade to Rails3/RSpec2. My views use partials pretty extensively >> and this issue is a huge blocker for me. >> >> before do >> view.should render_template("event_list", :locals => {:calendar => >> @calendar}) >> end >> >> causes all my related specs fail with: >> >> expecting <"event_list"> but rendering with <"">. >> Expected block to return true value. >> >> >> Any advice? > > In rspec-2/rails-3, view.should render_template("event_list") is no longer a message expectation (pre-action), but rather delegates to Rails'' assert_template method, so it needs to come _after_ the action. It should work exactly as you have it above, just after the action. > > This is documented in the rspec-rails README (https://github.com/rspec/rspec-rails - see sections on View Specs and Matchers), though not with an upgrade note pointing out that it changed. I''ll make that addition shortly. > > As for stubbing the template, so that it is not actually rendered, RSpec doesn''t offer a way to do this in view specs and I don''t think Rails does either. I added an issue for this (https://github.com/rspec/rspec-rails/issues/issue/263), and hopefully we''ll be able to resolve it soon. In the mean time, the examples will need to supply any data needed by the template being spec''d _and_ any of the partials. > > Side note: I urge you not to put message expectations (pre "When") or state-based expectations (post "When") in before/after blocks. It makes examples difficult to understand, and results in specifying the same things in every example in a group. Rather, have one example that specifies each individual thing. In this case: > > it "renders the event_list with @calendar" do > render > view.should render_template( > "event_list", > :locals => {:calendar => @calendar} > ) > endFYI - it looks like you can do the following before the action, but you''d be using an internal method of Rails which is not guaranteed to work the same way in the future: view.should_receive(:_render_partial).with( :partial => "event_list", :locals => { :calendar => @calendar } ) Do not use this unless you are willing to absorb the risk that it could change in a future Rails release without any warning. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101117/aafdbc67/attachment-0001.html>