ruby-on-rails - CircleCI:涉及时间戳的规范错误

标签 ruby-on-rails rspec circleci

我有一个返回 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 的初步 react ,上述方法实际上是他们推荐的方法。不过,他们还无法解释为什么要进行四舍五入。

更新 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 的额外解决方法(在测试中舍入断言)。 .

关于ruby-on-rails - CircleCI:涉及时间戳的规范错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30139038/

相关文章:

mysql - Ruby on Rails add_column 不会将该列添加到现有行

ruby-on-rails - 如何在 session 或Flash中传递表单错误? [ rails 2.3.5]

ruby-on-rails - Rails Active Record 在 before_create 回调中取消(保存或创建)而不引发异常

ruby - Serverspec 没有正确检查包版本

node.js - CircleCI:使用nodejs版本12

ruby-on-rails - 在 CircleCI 中设置 Elasticsearch 和 Ruby on Rails

ruby-on-rails - 查找日期时间与今天日期匹配的记录 - Ruby on Rails

ruby-on-rails - Rails secrets.yml VS Dotenv VS Figaro with Capistrano on AWS

ruby-on-rails - 如何在 RSPEC 中多次重复使用相同的测试

android - 如何将 firebase 测试实验室与 circleci 集成