FactoryGirl 为 Rails 模型时间属性设置了错误的时间
FactoryGirl setting wrong time for Rails model time attribute
给定一个具有架构的 ActiveRecord 模型,
create_table "foos", id: :serial, force: :cascade do |t|
t.date "date"
t.time "time"
# ...
还有一个来自 FactoryGirl 的 Foo
工厂,
FactoryGirl.define do
factory :foo do
date Date.today
time Time.new(2001, 9, 11)
# ...
和一个 rspec 使用 shoulda-matcher 的模型规范,
it { is_expected.to have_attributes(
date: Date.today,
time: Time.new(2001, 9, 11),
# ...
测试失败,
-:time => 2001-09-11 00:00:00.000000000 -0700,
+:time => 2000-01-01 08:00:00.000000000 +0000,
因为被测对象有一个time
属性,其值为2000-01-01 08:00:00.000000000 +0000
.
为什么 FactoryGirl 不使用工厂指定的时间值 Time.new(2001, 9, 11)
?
可能是因为 time
字段忽略了存储在数据库中的数据的日期部分。因此,您的测试需要测试该字段的 hhmmss
是否符合您的预期 - 但您也在检查 yymmdd
,并且看起来区域也在行动。
给定一个具有架构的 ActiveRecord 模型,
create_table "foos", id: :serial, force: :cascade do |t|
t.date "date"
t.time "time"
# ...
还有一个来自 FactoryGirl 的 Foo
工厂,
FactoryGirl.define do
factory :foo do
date Date.today
time Time.new(2001, 9, 11)
# ...
和一个 rspec 使用 shoulda-matcher 的模型规范,
it { is_expected.to have_attributes(
date: Date.today,
time: Time.new(2001, 9, 11),
# ...
测试失败,
-:time => 2001-09-11 00:00:00.000000000 -0700,
+:time => 2000-01-01 08:00:00.000000000 +0000,
因为被测对象有一个time
属性,其值为2000-01-01 08:00:00.000000000 +0000
.
为什么 FactoryGirl 不使用工厂指定的时间值 Time.new(2001, 9, 11)
?
可能是因为 time
字段忽略了存储在数据库中的数据的日期部分。因此,您的测试需要测试该字段的 hhmmss
是否符合您的预期 - 但您也在检查 yymmdd
,并且看起来区域也在行动。