ruby-on-rails - 如何通过删除 Time.Now 对出生日期格式进行 Rspec 测试?

标签 ruby-on-rails ruby rspec stub

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

验证 :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

最佳答案

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

关于ruby-on-rails - 如何通过删除 Time.Now 对出生日期格式进行 Rspec 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44119852/

相关文章:

rspec - 铁轨/ capybara : How to click a link in a table row that also contains unique text

ruby-on-rails - rails : redirect_to specific Twitter Bootstrap tab

javascript - bootstrap下拉菜单不下拉?

ruby-on-rails - 安装pg(0.15.1)时出错,Bundler无法继续

ruby - Dropbox API - Dropbox ruby​​ gem (蒂姆·摩根) : How to authorize

jquery - Capybara jQuery 脚本适用于 Selenium 驱动程序,但不适用于 Poltergeist

ruby-on-rails - 部分加载 Rails 以运行规范

HTML 问题 : form causing line break before (Rails button_to)

ruby - 使用 Ruby,我如何获取字符串并在单词之间插入竖线?

c# - 使用javascript在网页上自动登录和解析