CircleCI:涉及时间戳的规范错误
CircleCI: error with a spec involving timestamps
我有一个方法规范,returns ActiveRecord 对象的时间戳。
规范在本地通过,但每当它在 CircleCI 上 运行 时,预期与实际之间存在轻微的不匹配。
规范看起来像这样:
describe '#my_method' do
it 'returns created_at' do
object = FactoryGirl.create(:something)
expect(foo.bar(object)).to eq object.created_at
end
end
虽然它在本地通过,但在 CircleCI 上,我不断收到类似的错误消息。
示例如下:
(1)
expected: 2015-05-09 10:42:59.752192641 +0000
got: 2015-05-09 10:42:59.752192000 +0000
(2)
expected: 2015-05-08 10:16:36.777541226 +0000
got: 2015-05-08 10:16:36.777541000 +0000
从错误来看,我怀疑 CircleCI 正在对时间戳值进行四舍五入,但我没有足够的信息。有什么建议吗?
我遇到了同样的问题,目前有一张 CircleCI 的公开票以获取更多信息。当我知道更多时,我会更新这个答案。
与此同时,让这些测试通过的解决方法只是确保您在这样的测试中使用的时间戳使用模拟时间的库(如 timecop
)四舍五入。
describe '#my_method' do
it 'returns created_at' do
# CircleCI seems to round milliseconds, which can result in
# slight differences when serializing times.
# To work around this, ensure the millseconds end in 000.
Timecop.freeze(Time.local(2015)) do
object = FactoryGirl.create(:something)
expect(foo.bar(object)).to eq object.created_at
end
end
end
更新: 根据 CircleCI 的初始响应,上述方法实际上是他们推荐的方法。不过,他们还不能给我一个解释,说明为什么会发生四舍五入。
UPDATE 2: 看来这与不同系统之间的精度差异有关。我个人在 OS X 上看到了这个问题。这是 Circle 的回复:
From what I know, Time.now actually has different precision on OS X
and Linux machines. I would suppose that you will get the exact same
result on other Linux hosts, but all OS X hosts will give you the
result without rounding. I might be wrong, but I recall talking about
that with another customer. Mind checking that on a VM or an EC2
instance running Linux?
In the Time reference you can search for precision on the page—the
round method can actually adjust the precision for you. Would it be an
option for you to round the time in the assertion within the test?
我还没有尝试他们的建议来确认,但这似乎确实提供了一种不需要 timecop
.
您还可以在架构中定义时间列的精度,这样 ActiveRecord 将负责在类型转换时截断精度。
我有一个方法规范,returns ActiveRecord 对象的时间戳。
规范在本地通过,但每当它在 CircleCI 上 运行 时,预期与实际之间存在轻微的不匹配。
规范看起来像这样:
describe '#my_method' do
it 'returns created_at' do
object = FactoryGirl.create(:something)
expect(foo.bar(object)).to eq object.created_at
end
end
虽然它在本地通过,但在 CircleCI 上,我不断收到类似的错误消息。
示例如下:
(1)
expected: 2015-05-09 10:42:59.752192641 +0000
got: 2015-05-09 10:42:59.752192000 +0000
(2)
expected: 2015-05-08 10:16:36.777541226 +0000
got: 2015-05-08 10:16:36.777541000 +0000
从错误来看,我怀疑 CircleCI 正在对时间戳值进行四舍五入,但我没有足够的信息。有什么建议吗?
我遇到了同样的问题,目前有一张 CircleCI 的公开票以获取更多信息。当我知道更多时,我会更新这个答案。
与此同时,让这些测试通过的解决方法只是确保您在这样的测试中使用的时间戳使用模拟时间的库(如 timecop
)四舍五入。
describe '#my_method' do
it 'returns created_at' do
# CircleCI seems to round milliseconds, which can result in
# slight differences when serializing times.
# To work around this, ensure the millseconds end in 000.
Timecop.freeze(Time.local(2015)) do
object = FactoryGirl.create(:something)
expect(foo.bar(object)).to eq object.created_at
end
end
end
更新: 根据 CircleCI 的初始响应,上述方法实际上是他们推荐的方法。不过,他们还不能给我一个解释,说明为什么会发生四舍五入。
UPDATE 2: 看来这与不同系统之间的精度差异有关。我个人在 OS X 上看到了这个问题。这是 Circle 的回复:
From what I know, Time.now actually has different precision on OS X and Linux machines. I would suppose that you will get the exact same result on other Linux hosts, but all OS X hosts will give you the result without rounding. I might be wrong, but I recall talking about that with another customer. Mind checking that on a VM or an EC2 instance running Linux?
In the Time reference you can search for precision on the page—the round method can actually adjust the precision for you. Would it be an option for you to round the time in the assertion within the test?
我还没有尝试他们的建议来确认,但这似乎确实提供了一种不需要 timecop
.
您还可以在架构中定义时间列的精度,这样 ActiveRecord 将负责在类型转换时截断精度。