in Education by
I'm running Rails 3.1.1, RSpec 2.7.0 and HAML 3.1.3. Say I have the following view files: app/views/layouts/application.html.haml !!! %html %head %title Test = stylesheet_link_tag "application" = javascript_include_tag "application" = csrf_meta_tags %body = content_for?(:content) ? yield(:content) : yield app/views/layouts/companies.html.haml - content_for :content do #main = yield :main #sidebar = yield :sidebar = render :template => 'layouts/application' app/views/companies/index.html.haml - content_for :main do %h1 MainHeader - content_for :sidebar do %h1 SidebarHeader And the following spec file: spec/views/companies/index_spec.rb require 'spec_helper' describe 'companies/index.html.haml' do it 'should show the headers' do render rendered.should contain('MainHeader') rendered.should contain('SidebarHeader') end end When I run RSpec, I get the following error: 1) companies/index.html.haml should show the headers Failure/Error: rendered.should contain('MainHeader') expected the following element's content to include "MainHeader": # ./spec/views/companies/index_spec.rb:7:in `block (2 levels) in ' At first, I thought RSpec was somehow missing the content_for blocks when rendering the view files. However, I was not able to find any issue related to it on RSpec's github repository, so I'm not sure who's to blame here. One (recent) solution I found is at http://www.dixis.com/?p=571. However, when I try the suggested code view.instance_variable_get(:@_content_for) it returns nil. Is there a way to test content_for in view specs? Is there a better way to structure my layout files, such that I'm actually able to test them and still achieve the same end result? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Using Rspec 2 with Rails 3, in order to write view specs for usage of content_for, do this: view.content_for(:main).should contain('MainHeader') # instead of contain() I'd recommend using have_tag (webrat) # or have_selector (capybara) p.s. the value of a content_for(...) block by default is an empty string, so if you want to write specs showing cases in which content_for(:main) does not get called, use: view.content_for(:main).should be_blank Your spec could be written as: it "should show the headers" do render view.content_for(:main).should contain('MainHeader') view.content_for(:side_header).should contain('SidebarHeader') end This way your spec shows exactly what your view does, independent of any layout. For a view spec, I think it's appropriate to test it in isolation. Is it always useful to write view specs? That's an open question. Instead if you want to write specs showing what the markup served to the user looks like, then you'll want either a request spec or a cucumber feature. A third option would be a controller spec that includes views. p.s. if you needed to spec a view that outputs some markup directly and delegates other markup to content_for(), you could do that this way: it "should output 'foo' directly, not as a content_for(:other) block" do render rendered.should contain('foo') view.content_for(:other).should_not contain('foo') end it "should pass 'bar' to content_for(:other), and not output 'bar' directly" do render rendered.should_not contain('bar') view.content_for(:other).should contain('bar') end That would probably be redundant, but I just wanted to show how render() populates rendered and view.content_for. "rendered" contains whatever output the view produces directly. "view.content_for()" looks up whatever content the view delegated via content_for().

Related questions

0 votes
    What is the difference between RSpec and Cucumber?...
asked Jul 20, 2021 in Technology by JackTerrance
0 votes
    When to use Rspec and when to use Cucumber?...
asked Nov 16, 2020 in Technology by JackTerrance
0 votes
    I am trying to build a Rails 3.1 app in which users can preview individual pages of documents or ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    I moved a web app that was using 1.8.7 to 1.9.2 and now I keep getting incompatible character ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I'd like to add a CMS and blog to a web app. One that won't get in the way. There's ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 28, 2022 in Education by JackTerrance
0 votes
    Does anyone know how to add another folder to the asset pipeline in Rails 3.1? I'd like to serve app/assets/ ... an asset path, in your application.rb file: config.assets.paths...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I'm upgrading an application from Rails 3.0 to 3.1 and found the following error showing up in my ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    I have a Rails app that mounts 3 engines. Each engines has their own db migrations with timestamp in ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I made a basic rails app with a simple pages controller with an index function and when I load the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Now that Rails 3 beta is out, I thought I'd have a look at rewriting an app I have just started ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    I'm using ActionMailer in my application and I want to use localized views for email templates. It seems ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    I am using spring-boot latest 2.1.3 release and want to start with testcases, but this is not ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    I am using spring-boot latest 2.1.3 release and want to start with testcases, but this is not ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    To configure the admin settings of my app I made a Admin controller and a AdminConfig model. The Admin ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    I am trying to figure out how to use concerns in my routes file. I have models called User, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
...