我的User
模型经过以下验证,并且正在使用 Shoulda Matchers Gem(这是该方法的确切页面):
validates_inclusion_of :birthday,
:in => Date.new(1850)..Time.now.years_ago(13).to_date,
:message => 'Sorry, you must be at least 13 years old to join.'
我正在使用 FactoryGirl 和 Rspec。我对我的User
模型进行了这个测试:
describe "valid user age" do
it { should ensure_inclusion_of(:birthday).in_range(13..150) }
end
FactoryGirl.define do
factory :user do
sequence(:first_name) { |n| "Bob#{n}" }
sequence(:last_name) { |n| "User#{n}" }
email { "#{first_name}@example.com" }
birthday { Date.today - 13.years }
password "foobarbob"
end
end
现在从所有这些中我得到了错误:
User valid user age
Failure/Error: it { should ensure_inclusion_of(:birthday).in_range(13..150) }
Did not expect errors to include "is not included in the list" when birthday is set to 12, got error:
为什么在浏览器中测试时会出现这种情况?