Hi Hans,<br><br>I rewrote my specs the way you suggested and now everything works!<br><br>For those who are interested, here's how the code looks like now (after following Hans' tips).<br><br>> a) writing a separate spec for the partial _question_for_candidate
<br><br>require File.dirname(__FILE__) + '/../../spec_helper'<br><br>module QuestionForCandidatePartialHelper<br> def get_question_type_mock(type)<br> mock_model(QuestionType, {:name => type})<br> end<br><br>
def render_question_for_candidate_partial<br> render(:partial => 'survey/question_for_candidate', :locals => {:question => @question})<br> end<br>end<br><br>describe 'partial survey/_question_for_candidate.rhtml' do
<br> include QuestionForCandidatePartialHelper<br><br> before(:each) do<br> @question = mock_model(Question, {<br> :id => 1,<br> :description => 'The description',<br> :position => 4,<br>
:alternatives => [mock_model(Alternative, {:id => 1, :description => 'Alternative 1 Description'})],<br> })<br> @question_type_exclusive = get_question_type_mock('exclusive')<br> @question_type_multiple = get_question_type_mock('multiple')
<br> end<br><br> it 'should render the top of the template no matter the question type' do<br> @question.stub!(:question_type).and_return(@question_type_exclusive)<br> render_question_for_candidate_partial
<br> response.should have_tag('div.question.question-4') do<br> with_tag('.heading .number', '4.')<br> with_tag('.heading .description', @question.description)<br> end<br> end
<br><br> it 'should render an exclusive-answer question' do<br> @question.stub!(:question_type).and_return(@question_type_exclusive)<br> render_question_for_candidate_partial<br> response.should have_tag('
div.question.question-4') do<br> with_tag('input[type=?]', 'radio')<br> without_tag('input[type=?]', 'checkbox')<br> end<br> end<br><br> it 'should render a multiple-answer question' do
<br> @question.stub!(:question_type).and_return(@question_type_multiple)<br> render_question_for_candidate_partial<br> response.should have_tag('div.question.question-4') do<br> with_tag('input[type=?]', 'checkbox')
<br> without_tag('input[type=?]', 'radio')<br> end<br> end<br>end<br><br>> b) write a separate spec for the helper that renders the question<br><br>require File.dirname(__FILE__) + '/../spec_helper'
<br><br>describe SurveyHelper do<br> before(:each) do<br> @question = mock_model(Question)<br> end<br><br> it 'should render a question using the question_for_candidate partial' do<br> self.should_receive
(:render).with(:partial => 'question_for_candidate', :locals => {:question => @question})<br> render_question(@question)<br> end<br>end<br><br>> 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__) + '/../../spec_helper'<br><br>describe '/survey/show' do<br> before(:each) do<br> assigns[:configurations] = {:survey_name => 'Whatever'}
<br> @positions = [4, (9..22).to_a].flatten<br> @questions = Array.new<br> @positions.each do |position|<br> @questions[position] = mock_model(Question, {<br> :id => 1,<br> :description => 'The description',
<br> :position => 4,<br> :alternatives => [mock_model(Alternative, {:id => 1, :description => 'Description'})],<br> :question_type => mock_model(QuestionType, {:name => 'exclusive'})
<br> })<br> end<br> assigns[:questions] = @questions<br> end<br><br> it "should render the 'survey/question_for_candidate' partial" do<br> args = {:partial => 'question_for_candidate', :locals => {:question => an_instance_of(Question)}}
<br> template.should_receive(:render).with(args).exactly(@questions.nitems).times<br> render 'survey/show'<br> end<br>end<br><br>c) not use any fixtures in the view specs but instead use mocks for
<br>that: it'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 <<a href="mailto:hans@degraaff.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">hans@degraaff.org
</a>> 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'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'm afraid that I don't see immediately why your current setup won'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>> before(:each) do<br>> assigns[:configurations] = {:survey_name => 'Whatever'}
<br>> assigns[:questions] = Array.new<br>> assigns[:questions][4] = questions(:faixa_etaria)<br><br></div>I am a little bit suspicious about this construction, though. I'm not<br>sure whether assigns is a normal Array in this case, so I'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