Hi,<br><br>I've been trying to find an answer for this problem in the last couple hours, but I think no discussion was about this exact same thing. So here it goes, hope someone can help.<br><br>I'm trying to spec a view which
<span style="font-style: italic;">works correctly on the browser</span>, but that generates the following error when I run "rake spec:views".<br><br>ActionView::TemplateError in '/survey/show should display the "question 4" heading correctly'
<br>You have a nil object when you didn't expect it!<br>The error occurred while evaluating nil.position<br>On line #1 of app/views/survey/_question_for_candidate.rhtml<br><br> 1: <div class="question question-<%=
question.position %>"><br> 2: <p class="heading"><br> 3: <span class="number"><%= question.position %>.</span><br> 4: <span class="description"><%=
question.description %></span><br><br>RSpec is telling me that the <span style="font-style: italic;">"question" object is nil</span>. I can't figure it out why.<br><br>Here's the spec that generates the error (the "it" block should test some tags inside the "response" object):
<br><br>require File.dirname(__FILE__) + '/../../spec_helper'<br><br>describe '/survey/show' do<br> fixtures 'questions', 'alternatives'<br><br> before(:each) do<br> assigns[:configurations] = {:survey_name => 'Whatever'}
<br> assigns[:questions] = Array.new<br> assigns[:questions][4] = questions(:faixa_etaria)<br> @faixa_etaria = questions(:faixa_etaria)<br> end <br><br> it 'should display the "question 4" heading correctly' do
<br> render 'survey/show'<br> end <br>end<br><br>Here's the :faixa_etaria Question fixture I'm using:<br><br>faixa_etaria:<br> id: 1<br> question_type_id: 1<br> description: 'Em qual das faixas etárias abaixo você se inclui?'
<br> position: 4<br><br>(The description value is in Portuguese).<br><br>Here's the "show" method inside the SurveyController:<br><br> def show<br> @configurations = {}<br> Configuration.find(:all).each { |c| @configurations[
c.name.to_sym] = c.value }<br> @questions = {}<br> Question.find(:all).each { |q| @questions[q.position] = q } <br> end <br clear="all"><br><br>Here's the piece of code inside the "show.rhtml" template that's calling the helper method that will cause that error:
<br><br><%= render_question(@questions[4]) %><br><br>Here's the "render_question" method implementation, inside the SurveyHelper class (<span style="font-style: italic;">invoking "render" seems to be the problem, but I don't know why
</span>):<br><br> def render_question(question)<br> render(:partial => 'question_for_candidate', :locals => {:question => question})<br> end <br><br><span style="font-style: italic;">The :locals Hash has :question as a key, and that is the variable that RSpec is complaining about inside the partial
</span>.<br><br>Finally, here's the "question_for_candidate" partial:<br><br><div class="question question-<%= question.position %>"><br> <p class="heading"><br> <span class="number"><%=
question.position %>.</span><br> <span class="description"><%= question.description %></span><br> </p><br> <% for alternative in question.alternatives -%> <br> <%= radio_button 'candidate', "question_#{
<a href="http://question.id">question.id</a>}", <a href="http://alternative.id">alternative.id</a> %><br> <label for="candidate_question"><%= alternative.description %></label><br> <br />
<br> <% end -%> <br></div><br><br>I'm not that experienced with Rails nor RSpec, so I'm totally lost about this problem.<br><br>-- <br>Caio Moritz Ronchi