Hi Hans,<br><br>I rewrote my specs the way you suggested and now everything works!<br><br>For those who are interested, here&#39;s how the code looks like now (after following Hans&#39; tips).<br><br>&gt; a) writing a separate spec for the partial _question_for_candidate
<br><br>require File.dirname(__FILE__) + &#39;/../../spec_helper&#39;<br><br>module QuestionForCandidatePartialHelper<br>&nbsp; def get_question_type_mock(type)<br>&nbsp;&nbsp;&nbsp; mock_model(QuestionType, {:name =&gt; type})<br>&nbsp; end<br><br>
&nbsp; def render_question_for_candidate_partial<br>&nbsp;&nbsp;&nbsp; render(:partial =&gt; &#39;survey/question_for_candidate&#39;, :locals =&gt; {:question =&gt; @question})<br>&nbsp; end<br>end<br><br>describe &#39;partial survey/_question_for_candidate.rhtml&#39; do
<br>&nbsp; include QuestionForCandidatePartialHelper<br><br>&nbsp; before(:each) do<br>&nbsp;&nbsp;&nbsp; @question = mock_model(Question, {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :id =&gt; 1,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :description =&gt; &#39;The description&#39;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :position =&gt; 4,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :alternatives =&gt; [mock_model(Alternative, {:id =&gt; 1, :description =&gt; &#39;Alternative 1 Description&#39;})],<br>&nbsp;&nbsp;&nbsp; })<br>&nbsp;&nbsp;&nbsp; @question_type_exclusive = get_question_type_mock(&#39;exclusive&#39;)<br>&nbsp;&nbsp;&nbsp; @question_type_multiple = get_question_type_mock(&#39;multiple&#39;)
<br>&nbsp; end<br><br>&nbsp; it &#39;should render the top of the template no matter the question type&#39; do<br>&nbsp;&nbsp;&nbsp; @question.stub!(:question_type).and_return(@question_type_exclusive)<br>&nbsp;&nbsp;&nbsp; render_question_for_candidate_partial
<br>&nbsp;&nbsp;&nbsp; response.should have_tag(&#39;div.question.question-4&#39;) do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; with_tag(&#39;.heading .number&#39;, &#39;4.&#39;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; with_tag(&#39;.heading .description&#39;, @question.description)<br>&nbsp;&nbsp;&nbsp; end<br>&nbsp; end
<br><br>&nbsp; it &#39;should render an exclusive-answer question&#39; do<br>&nbsp;&nbsp;&nbsp; @question.stub!(:question_type).and_return(@question_type_exclusive)<br>&nbsp;&nbsp;&nbsp; render_question_for_candidate_partial<br>&nbsp;&nbsp;&nbsp; response.should have_tag(&#39;
div.question.question-4&#39;) do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; with_tag(&#39;input[type=?]&#39;, &#39;radio&#39;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; without_tag(&#39;input[type=?]&#39;, &#39;checkbox&#39;)<br>&nbsp;&nbsp;&nbsp; end<br>&nbsp; end<br><br>&nbsp; it &#39;should render a multiple-answer question&#39; do
<br>&nbsp;&nbsp;&nbsp; @question.stub!(:question_type).and_return(@question_type_multiple)<br>&nbsp;&nbsp;&nbsp; render_question_for_candidate_partial<br>&nbsp;&nbsp;&nbsp; response.should have_tag(&#39;div.question.question-4&#39;) do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; with_tag(&#39;input[type=?]&#39;, &#39;checkbox&#39;)
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; without_tag(&#39;input[type=?]&#39;, &#39;radio&#39;)<br>&nbsp;&nbsp;&nbsp; end<br>&nbsp; end<br>end<br><br>&gt; b) write a separate spec for the helper that renders the question<br><br>require File.dirname(__FILE__) + &#39;/../spec_helper&#39;
<br><br>describe SurveyHelper do<br>&nbsp; before(:each) do<br>&nbsp;&nbsp;&nbsp; @question = mock_model(Question)<br>&nbsp; end<br><br>&nbsp; it &#39;should render a question using the question_for_candidate partial&#39; do<br>&nbsp;&nbsp;&nbsp; self.should_receive
(:render).with(:partial =&gt; &#39;question_for_candidate&#39;, :locals =&gt; {:question =&gt; @question})<br>&nbsp;&nbsp;&nbsp; render_question(@question)<br>&nbsp; end<br>end<br><br>&gt; b) in the view spec for /survey/show use should_receive to determine
<br>that the helper is actually being called<br><br>require File.dirname(__FILE__) + &#39;/../../spec_helper&#39;<br><br>describe &#39;/survey/show&#39; do<br>&nbsp; before(:each) do<br>&nbsp;&nbsp;&nbsp; assigns[:configurations] = {:survey_name =&gt; &#39;Whatever&#39;}
<br>&nbsp;&nbsp;&nbsp; @positions = [4, (9..22).to_a].flatten<br>&nbsp;&nbsp;&nbsp; @questions = Array.new<br>&nbsp;&nbsp;&nbsp; @positions.each do |position|<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @questions[position] = mock_model(Question, {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :id =&gt; 1,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :description =&gt; &#39;The description&#39;,
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :position =&gt; 4,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :alternatives =&gt; [mock_model(Alternative, {:id =&gt; 1, :description =&gt; &#39;Description&#39;})],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :question_type =&gt; mock_model(QuestionType, {:name =&gt; &#39;exclusive&#39;})
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; })<br>&nbsp;&nbsp;&nbsp; end<br>&nbsp;&nbsp;&nbsp; assigns[:questions] = @questions<br>&nbsp; end<br><br>&nbsp; it &quot;should render the &#39;survey/question_for_candidate&#39; partial&quot; do<br>&nbsp;&nbsp;&nbsp; args = {:partial =&gt; &#39;question_for_candidate&#39;, :locals =&gt; {:question =&gt; an_instance_of(Question)}}
<br>&nbsp;&nbsp;&nbsp; template.should_receive(:render).with(args).exactly(@questions.nitems).times<br>&nbsp;&nbsp;&nbsp; render &#39;survey/show&#39;<br>&nbsp; end<br>end<br><br>c) not use any fixtures in the view specs but instead use mocks for
<br>that: it&#39;s faster and you can check exactly what needs to happen instead<br>of relying on what happens to be in the fixture.<br><br>As you can see mocks are now all over the place. No fixtures were used ;)<br><br>
Thanks a lot,<br><br>Caio<br><br><div class="gmail_quote">On Nov 11, 2007 6:01 AM, Hans de Graaff &lt;<a href="mailto:hans@degraaff.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">hans@degraaff.org
</a>&gt; wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
You are trying to test a lot of things at the same time, which is one of<br>the reasons that it is now hard to diagnose a problem.<br><br>I would tackle this by<br>a) writing a separate spec for the partial _question_for_candidate
<br>b) write a separate spec for the helper that renders the question<br>b) in the view spec for /survey/show use should_receive to determine<br>that the helper is actually being called<br>c) not use any fixtures in the view specs but instead use mocks for
<br>that: it&#39;s faster and you can check exactly what needs to happen instead<br>of relying on what happens to be in the fixture.<br><br>I&#39;m afraid that I don&#39;t see immediately why your current setup won&#39;t
<br>
work, but untangling things along the lines sketched above should<br>hopefully get you to a situation where it becomes easier to see what<br>happens.<br><div><br>&gt; &nbsp; before(:each) do<br>&gt; &nbsp; &nbsp; assigns[:configurations] = {:survey_name =&gt; &#39;Whatever&#39;}
<br>&gt; &nbsp; &nbsp; assigns[:questions] = Array.new<br>&gt; &nbsp; &nbsp; assigns[:questions][4] = questions(:faixa_etaria)<br><br></div>I am a little bit suspicious about this construction, though. I&#39;m not<br>sure whether assigns is a normal Array in this case, so I&#39;d create the
<br>Array and populate it before handing it to assigns:<br><br>@questions = Array.new<br>@questions[4] = ...<br>assigns[:questions] = @questions<br><br>Kind regards,<br><font color="#888888"><br>Hans<br></font><br>_______________________________________________
<br>rspec-users mailing list<br><a href="mailto:rspec-users@rubyforge.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">rspec-users@rubyforge.org</a><br><a href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://rubyforge.org/mailman/listinfo/rspec-users
</a><br></blockquote></div><br><br clear="all"><br>-- <br>Caio Moritz Ronchi