CircleCI: error with a spec involving timestamps

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.


You can also define the precision of your time columns in your schema, this way ActiveRecord will take care of precision truncating at type cast.

See https://github.com/rails/rails/blob/39de1156970d52b441ff944862ebe16a47de784f/activemodel/lib/active_model/type/date_time.rb#L18