I have a few RSpec examples that I wrote to test various aspects of the rendered HTML as generated by a controller. These include things like...
it "should be successful" do
get :show, :section => @section.slug
response.should be_success
end
it "should have the right title" do
get :show, :section => @section.slug
response.should have_selector("title", :content => @section.name)
end
it "should find the right section" do
get :show, :section => @section.slug
assigns(:section).should == @section
end
These are testing SectionsController#show
. When I navigate to the page in my browser, it is rendered perfectly, with no exceptions raised. However, RSpec returns (for all three, mind you):
Failure/Error: get :show, :section => @section.slug
ActionView::Template::Error
This is obviously failing on the line get :show, :section => @section.slug
for all of the tests.
I have narrowed down the issue to one partial. In the view file for this action, I included a partial with
<%= render 'popular' %>
...and when this line is removed, all the tests pass. The relevant content for this partial is:
<div class="most_popular group">
<%= render post_excerpt(@popular.shift, :class => "one") %>
</div><!-- end most popular -->
That post_excerpt is a helper method I wrote to return some parameters as a hash to be passed to render
.
def post_excerpt(post, options)
{ :partial => 'posts/post_excerpt', :object => post, :as => :post, :locals => { :klass => options[:class] } }
end
开发者_运维问答
The partial 'posts/post_excerpt':
<article class="<%= klass if klass %>">
<h2><%= link_to post.name, section_post_path(post.section, post) %></h2>
<footer>By <%= link_to post.author.name, post.author %></footer>
<hr />
<div class="content">
<%= raw(post.excerpt) %>
</div><!-- end content -->
</article>
I am running RSpec 2.4.0 and Rails 3.0.3. I can provide more details or code snippets if needed, but I think this is enough to go on.
If you have any idea what might be causing this strange issue, please post it! Thanks!
精彩评论