1

適格と呼ばれるメソッドの4つの構成メソッドについて、条件を合格から不合格に変更するためのかなり複雑な設定があります。

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

これらの2つと同じようにさらに2つの方法があります。私がしたいのは抽出することです

 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 に答える