Need a means of stubbing partials #263

Closed
dchelimsky opened this Issue Nov 17, 2010 · 9 comments

Comments

Projects
None yet
2 participants
Owner

dchelimsky commented Nov 17, 2010

rspec-rails-1 supports stubbing and/or setting message expectations on templates that render partials. This keeps a spec for an edit template, for example, isolated from the content of the _form partial.

There is currently no easy way to do this in rspec-rails-2.

mdarby commented Nov 17, 2010

I will wash your car if this issue can be fixed. For real.

Owner

dchelimsky commented Nov 17, 2010

You can do this for now in your own specs. Need to properly spec and integrate it, but should be able to get stub_partial (or similar) in the 2.2 release.

def view.stub_partial(hash)
  view_paths.unshift(ActionView::FixtureResolver.new(hash))
end
view.stub_partial("gadgets/_form" => "")

mdarby commented Nov 17, 2010

Thanks David!

Where would you recommend placing this method? Placing it directly in a view spec returns this error when run:

undefined local variable or method `view' for #ActionView::Base:0x000001085bbc98

Owner

dchelimsky commented Nov 21, 2010

I'd put this bit in a global before hook:

config.before, :type => :view do
  def view.stub_partial(hash)
    view.view_paths.unshift(ActionView::FixtureResolver.new(hash))
  end
end

Then you can say

view.stub_partial("gadgets/_form" => "")

directly in a spec, or in a before hook.

mdarby commented Nov 22, 2010

I hate to keep bugging you, but the code then fails with:

undefined method `_routes' for nil:NilClass

It still seems that the view is being accessed before its being setup during the spec running.

My code:

before do
  view.stub_partial("calendar/_calendar" => "")
end
Owner

dchelimsky commented Nov 22, 2010

I get the same thing. It works if you define it directly in the example (per my first comment above), so do that for now and I'll have something built into the framework for the next release. It's likely to be called stub_template rather than stub_partial, but it's all undecided yet and I'm only mentioning it because you seem anxious to use something now.

mdarby commented Nov 22, 2010

Yep, I'm trying to upgrade a 400+ view app that uses partials pretty extensively and unfortunately, I may have relied on stubbing out partials too heavily when I originally developed it.

Thanks for your help and I'll look forward to the next release.

Owner

dchelimsky commented Nov 23, 2010

Add stub_template method for view specs.

  • supports stubbing partials rendered by views in view specs
  • Closed by 55acd0f.

mdarby commented Nov 23, 2010

Thank you so much for taking the time to implement this so quickly. I appreciate it.

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment