2012-11-05 4 views
1

나는 qualified라는 메소드의 4 가지 작성 메소드에 대해 실패에서 통과로 상태를 변경하는 꽤 정교한 설정을 가지고 있습니다.일련의 RSpec 유닛 테스트의 이전 블록에서 방법을 사용할 수 있습니까?

describe "#participant_age_eligible?" do 
     it "returns whether participant is age-eligible" do 
      @part.participant_age_eligible?(@pers).should == true 
     end 

     it "returns false if participant is not age eligible" do 
      q = @survey_section.questions.select { |q| q.data_export_identifier == 
               "#{OperationalDataExtractor::PbsEligibilityScreener:: 
                INTERVIEW_PREFIX}.AGE_ELIG" }.first 
      answer = q.answers.select { |a| a.response_class == "answer" && a.reference_identifier == "2" }.first 
      Factory(:response, :survey_section_id => @survey_section.id, :question_id => q.id, :answer_id => answer.id, :response_set_id => @response_set.id) 
      @part.participant_age_eligible?(@pers).should == false 
     end 
     end 

     describe "#participant_psu_county_eligible?" do 
     it "returns whether participant lives in eligible PSU" do 
      @part.participant_psu_county_eligible?(@pers).should == true 
     end 

     it "returns false if participant coes not live in an eligible PSU" do 
      q = @survey_section.questions.select { |q| q.data_export_identifier == 
               "#{OperationalDataExtractor::PbsEligibilityScreener:: 
                INTERVIEW_PREFIX}.PSU_ELIG_CONFIRM" }.first 
      answer = q.answers.select { |a| a.response_class == "answer" && a.reference_identifier == "2" }.first 
      Factory(:response, :survey_section_id => @survey_section.id, :question_id => q.id, :answer_id => answer.id, :response_set_id => @response_set.id) 
      @part.participant_psu_county_eligible?(@pers).should == false 
     end 
     end 

두 가지 방법이 더 있습니다. 내가 뭘하려는 관련 매개 변수를 전달 다음 블록 이전에하는 방법에

q = @survey_section.questions.select { |q| q.data_export_identifier == 
                "#{OperationalDataExtractor::PbsEligibilityScreener:: 
                 INTERVIEW_PREFIX}.AGE_ELIG" }.first 
       answer = q.answers.select { |a| a.response_class == "answer" && a.reference_identifier == "2" }.first 
       Factory(:response, :survey_section_id => @survey_section.id, :question_id => q.id, :answer_id => answer.id, :response_set_id => @response_set.id) 

부분을 추출하고,하지만 난 누군가, 블록 앞에있는 방법을하지 정의 본 적이 있기 때문에 나는 hesitiate 당신이 그것을 할 수 있다고 확신 할지라도, 더 나아가, 당신이 할 수있다하더라도 그것을해야하는지조차 모르겠다. 아마 내가 보지 못하는 문제를 지적하고 있을지 모른다. 그래서 나는 SO 커뮤니티에 대한 엄청난 전문성을 묻겠지 만. 감사.

답변

1

특정 rspec 파일에서 메소드를 정의한 다음 해당 파일의 아무 곳에서나 호출 할 수 있습니다. 예를 들어 :

# your_file_spec.rb 
describe MyModel do 
    before(:each) { setup_variables } 

    describe ... 
    end 

    # I usually put my helper methods at the bottom 
    def setup_variables 
    # Do some work 
    end 
end 

당신은 또한 때때로 예를 들어 작업에 '시나리오 개요'접근 방식을 사용할 수는 :

# your_file_spec.rb 
describe MyModel do 
    examples = [{:name => "Joe", :login => "joe18"}, {:name => "Grace", :login => "grace12"}] 

    examples.each do |example| 
    it "logs in #{example[:name]}." do 
     # Do some work 
    end 
    end 
end 

당신은 또한 유용을 찾을 수 있습니다.

+0

그래, 조금 이상하게 보였지만, 정의의 상류에있는 메소드를 호출하는 범위 지정 규칙을 위반하는 것과 비슷하지만 Rspec이 전체 파일을 먼저로드 할 수 있습니까? 감사. –

+0

모든 것이 먼저로드됩니다. 파일의 중간에있는'before (: each) '를 쓸 수 있으며, 가장 이해가되지 않더라도 여전히 작동 할 것입니다. 또한 다른 파일에 도우미 메서드를 작성하고'spec/support' 파일에 저장하고이를 도우미 도우미 또는 사양별로 포함 할 수 있습니다. – MrDanA

관련 문제