Search code examples
ruby-on-railsrubyminitest

Flaky test failures in Minitest due to UTC time mismatch


Can anyone shed any light on what is causing this test to randomly fail?

There's obviously a mismatch between the time formats but I don't understand why it's happening randomly.

Our test:-

  # MeetingControllerTest

  test 'returns correctly when page param is not set' do    
    30.times do |i|
      Meeting.create!(
        start_date: @meeting.start_date + (i + 1).days,
        end_date: @meeting.end_date + (i + 1).days,
        user: @meeting.user,
        calendar_event_id: @meeting.calendar_event_id
      )
    end

    get meetings_url,
        params: {
          summary: 'meeting',
          per_page: 5,
          page: nil
        },
        headers: { 'Authorization' => @authorization }
    assert_response :success
    response_body = JSON.parse(response.body)
    meetings = response_body['meetings']

    assert_equal((@meeting.start_date + 4.days).strftime('%FT%T.%LZ'), meetings.last['start_date'])
  end

The assertion failure is:-

--- expected
+++ actual
@@ -1 +1 @@
-"2021-09-06T16:19:14.000Z"
+"2021-09-06T16:19:14.000+01:00"

Solution

  • You should try assert_in_delta. Replace this:

    assert_equal((@meeting.start_date + 4.days).strftime('%FT%T.%LZ'), meetings.last['start_date'])
    

    To this:

    assert_in_delta((@meeting.start_date + 4.days).strftime('%FT%T.%LZ'), meetings.last['start_date'], 1.second)