CircleCI: error with a spec involving timestamps

I have a spec for a method that returns a timestamp of an ActiveRecord object.

The spec passes locally, but whenever it is run on CircleCI, there is a slight mismatch between the expected and the actual.

The spec looks something like this:

describe '#my_method' do
  it 'returns created_at' do
    object = FactoryGirl.create(:something)
    expect(foo.bar(object)).to eq object.created_at
  end
end

While it passes locally, on CircleCI, I continually get similar error messages.

Here are examples:

(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

From the error, I suspect that CircleCI is rounding up the timestamp value, but I do not have enough information. Any suggestions?


I am encountering the same issue and currently have an open ticket with CircleCI to get more information. I'll update this answer when I know more.

In the meantime, a workaround to get these tests passing is just to ensure that the timestamp you're working with in a test like this is rounded using a library that mocks time (like 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

UPDATE: Based on the initial response from CircleCI, the above approach is actually their recommended approach. They haven't been able to give me an explanation yet as to why the rounding is actually happening, though.

UPDATE 2: It looks like this has something to do with the precision difference between different systems. I'm personally seeing this issue on OS X. Here's the response from 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?

I have not tried their suggestions yet to confirm, but this does seem to provide an explanation as well as an additional workaround (rounding the assertion within the test) that doesn't require timecop .

链接地址: http://www.djcxy.com/p/85198.html

上一篇: 迭代器和const指针

下一篇: CircleCI:涉及时间戳的规格错误