如何通过剔除Time.Now对出生日期格式进行Rspec测试?

时间:2017-05-22 18:34:27

标签: ruby-on-rails ruby rspec stub

这是对出生日期的验证,我需要使用rspec进行测试。

validates :date_of_birth, date: { before: Proc.new { Date.today }, message: 'must be before today' }, on: :create

这就是我所做的,但似乎并不正确

it "should allow valid birth date" do
date_of_birth = Time.now
allow(Time).to receive(:now).and_return(date_of_birth)

date_of_birth.capture_item("date of birth")
expect(date_of_birth.items[0].date_captured).to eq(date_of_birth)
end

1 个答案:

答案 0 :(得分:1)

it 'should allow valid birth date' do
  person = Person.new(date_of_birth: Date.yesterday)
  person.valid?
  expect(person.errors[:date_of_birth]).to include('must be before today')
end