1

我有一个非常精细的设置来将条件从通过更改为失败的四个组合方法称为合格?。

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) 

部分到 before 块中的方法,然后传递相关参数,但我犹豫,因为我从未见过有人在 before 块中定义方法,甚至不确定你能做到,进一步,我不确定你是否甚至即使可以,也应该这样做,也许它指出了我没有看到的问题。所以我想问问 SO 社区深不可测的巨大专业知识。谢谢。

4

1 回答 1

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

您可能还会发现这很有用。

于 2012-11-05T14:07:38.843 回答