Skip to content
This repository

Need a means of stubbing partials #263

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

2 participants

David Chelimsky Matt Darby
David Chelimsky
Owner

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.

Matt Darby

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

David Chelimsky
Owner

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" => "")
Matt Darby

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

David Chelimsky
Owner

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.

Matt Darby

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
David Chelimsky
Owner

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.

Matt Darby

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.

David Chelimsky
Owner

Add stub_template method for view specs.

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

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

Akira Matsuda amatsuda referenced this issue from a commit November 22, 2010
David Chelimsky Add stub_template method for view specs.
- supports stubbing partials rendered by views in view specs
- Closes #263.
55acd0f
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.