Unit testing 我如何为reject_body_scores(属性化)方法编写测试代码? class Horse:毁灭 接受:body_分数的_嵌套_属性,:reject_if=>:reject_body_分数 私有的 def拒绝_身体_分数(归因) 新纪录属性化['date']。空白?| |属性化['score'],空白? 结束 结束

Unit testing 我如何为reject_body_scores(属性化)方法编写测试代码? class Horse:毁灭 接受:body_分数的_嵌套_属性,:reject_if=>:reject_body_分数 私有的 def拒绝_身体_分数(归因) 新纪录属性化['date']。空白?| |属性化['score'],空白? 结束 结束,unit-testing,rspec,Unit Testing,Rspec,及 class BodyScoretrue 结束 类似的内容: class BodyScore < ActiveRecord::Base attr_accessible :horse_id, :score, :scoring_date belongs_to :horse validates :horse_id, :score, :scoring_date, :presence => true end 你应该涵盖所有可能的行为 我还使用了object.send测试所

class BodyScoretrue
结束
类似的内容:

class BodyScore < ActiveRecord::Base

  attr_accessible :horse_id, :score, :scoring_date
  belongs_to :horse

  validates :horse_id, :score, :scoring_date, :presence => true

end
你应该涵盖所有可能的行为

我还使用了
object.send
测试所描述的私有方法的技巧

upd: 由于您是测试新手,我将添加一些关于测试的描述

我用它来创建新工厂,并用它


我习惯于在块之前分配新变量,而不是

谢谢你的回复。我在编写测试代码方面是新手,所以这有助于我更好地理解。
class BodyScore < ActiveRecord::Base

  attr_accessible :horse_id, :score, :scoring_date
  belongs_to :horse

  validates :horse_id, :score, :scoring_date, :presence => true

end
  describe "#reject_body_scores" do
    context "when record is new" do
      let(:horse) { build :horse }
      let(:options) { {} }
      it "reject body" do
        horse.send(:reject_body_scores, options).should be_true
      end
    end

    context "when date blank" do
      let(:horse) { create :horse }
      let(:options) { {} }
      it "reject body" do
        horse.send(:reject_body_scores, options).should be_true
      end
    end

    context "when score blank" do
      let(:horse) { create :horse }
      let(:options) { { "date" => Date.current } }
      it "reject body" do
        horse.send(:reject_body_scores, options).should be_true
      end
    end

    context "when date and score present" do
      let(:horse) { create :horse }
      let(:options) { { "date" => Date.current, "score" => 5 } }
      it "don't reject body" do
        horse.send(:reject_body_scores, options).should be_false
      end
    end
  end