0

我有一个模型处理这些属性:

  • prelaunch_date
  • 发射日期
  • 结束日期

我使用“验证及时性” gem 在我的 models/deal.rb 中验证发布日期在预发布日期和 end_date 之间:

 # Validates order of dates
 validates_datetime :game_launch_date, :after => :prelaunch_date
 validates_datetime :game_end_date, :after => :game_launch_date

我现在需要用 rspec 测试它。我怎样才能做到这一点 ? 我尝试了以下方法,但它不起作用:

describe "validations" do
it { should_not allow_value(:prelaunch_date - 1.day).for(:launch_date) }
it { should_not allow_value(:end_date + 1.day).for(:game_launch_date) }

end
4

2 回答 2

1

你有这个想法,但似乎期待太多的语法魔法。例如,您不能对此进行评估::prelaunch_date - 1.day.

对于游戏发布日期验证,您可以执行以下操作:

describe "validation" do
  context "of game launch date" do
    let(:date) { Date.new(2013, 1, 2) }
    subject { described_class.new(:prelaunch_date => date) }

    it "requires game launch to be after prelaunch" do
      should_not allow_value(date - 1.day).for(:game_launch_date)
      should_not allow_value(date).for(:game_launch_date)
      should allow_value(date + 1.day).for(:game_launch_date)
    end
  end
end
于 2013-09-25T08:21:32.517 回答
0

您可能需要更多地手动测试它。可以像这样测试验证设置是否正确:

let(:deal) { Deal.new(:pre_launch_date => Date.today) }

it "must have game launch date after pre launch date" do
  deal.game_launch_date = 1.day.ago
  deal.valid?
  expect(deal.errors[:game_launch_date]).to_not be_empty
end
于 2013-09-21T12:08:52.067 回答