Dylan Markow
2010-Aug-18 22:39 UTC
[rspec-users] RSpec 2/Rails 3 - content_for in view specs
My main layout includes separate content_for/yield sections for my header, sidebar, footer, and content. However, when running a view spec, the `rendered` variable seems to only contain a string of just the primary content and ignores the header/footer/sidebar as well as the rest of my layout file (the "render" call just returns my primary content wrapped in generic <html> and <body> tags). Is there a way to get to the content captured in a "content_for" block through my view specs, or at the very least get my "render" call to use the full layout? I''ve tried doing a "render :layout => ''application''" which gives me: 1) contacts/show.html.erb shows a URL if the contact has one Failure/Error: render :layout => "application" undefined method `formats'' for nil:NilClass # /home/dylan/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0.rc/lib/active_support/whiny_nil.rb:48:in `method_missing'' # /home/dylan/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_view/render/rendering.rb:25:in `render'' # /home/dylan/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_view/test_case.rb:103:in `render'' # /home/dylan/.rvm/gems/ruby-1.9.2-p0/gems/rspec-rails-2.0.0.beta.19/lib/rspec/rails/example/view_example_group.rb:73:in `render'' # ./spec/views/contacts/show.html.erb_spec.rb:20:in `block (2 levels) in <main>'' I''ve also tried using response.capture(:sidebar), response[:sidebar], rendered.capture(:sidebar), rendered[:sidebar], view.capture(:sidebar), etc. and none of them worked -- they all give various errors. Here is my view spec that I''m trying to run: describe "contacts/show.html.erb" do it "shows a URL if present" do @contact = Factory(:complete_contact, :web_page => " http://www.example.com") # Using Factory Girl render rendered.should have_selector("span", :content => " http://www.example.com") end end Thank you! Dylan Markow dmarkow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100818/5ae5b44c/attachment.html>
Michael Schuerig
2010-Aug-28 23:12 UTC
[rspec-users] RSpec 2/Rails 3 - content_for in view specs
On Thursday 19 August 2010, Dylan Markow wrote:> Is there a way to get to the content captured in a "content_for" > block through my view specs, or at the very least get my "render" > call to use the full layout?Try defining a method like this in your spec def content_for(name) view.instance_variable_get(:@_content_for)[name] end and then content_for(:sidebar).should ... Michael -- Michael Schuerig mailto:michael at schuerig.de http://www.schuerig.de/michael/
This worked in Rails 3.1 but view.instance_variable_get(:@_content_for) is nil in Rails 3.2 Anybody have a solution for this in Rails 3.2? Thanks! Jared On Saturday, August 28, 2010 4:12:53 PM UTC-7, Michael Schuerig wrote:> > On Thursday 19 August 2010, Dylan Markow wrote: > > Is there a way to get to the content captured in a "content_for" > > block through my view specs, or at the very least get my "render" > > call to use the full layout? > > Try defining a method like this in your spec > > def content_for(name) > view.instance_variable_get(:@_content_for)[name] > end > > and then > > content_for(:sidebar).should ... > > > 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 > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120608/b25561fb/attachment.html>