mercredi 19 août 2015

(Rails) Test fails during late night time

My rails application is covered by many tests. All the tests pass in regular situations. That is, excluding when it's late at night.

There's actually a few tests that end up failing when it's night. All these tests involve modifying a model's time attribute and seeing if related models are affected.

test "changing time should affect hours" do
    // ..User is loaded.
    user.attend(event)
    assert_equal 1, user.hours // User attends a 1 hour event that has passed. 
    // Move event to the future.
    event.update_attributes(date: Date.today,
                              start_time: Time.now,
                              end_time: Time.now + 1.hour)
    assert_equal 0, attendance_of(user).hours // Passes in day, fails during night
  end

test "valid event creation" do
    // Count does NOT change by 1 at night.
    assert_difference '@group.events.count', 1 do
      post group_events_path(@group), event: { ...
                                                date: Date.today,
                                                start_time: Time.now,
                                                end_time: Time.now + 1.hour,
                                                ... }
    end
  end

What is going on here? For reference, here's what I currently use to determine when to update an attendance (which is something that an event has). This comes from the event controller:

  def not_ended?
    date.future? || (date.today? &&
      (Time.now.seconds_since_midnight < end_time.seconds_since_midnight))
  end

  def update_attendances
    // ... Determine the new date, start, and end time values through ActiveRecord::Dirty
    if not_ended?
      remove_checks = true
    end
    attendances.each do |attendance|
      new_checked = remove_checks ? false : attendance.checked
      attendance.update_attributes(went: new_start, left: new_end,
                                    checked: new_checked)
    end
  end
end

Validating an event to make sure its times aren't weird:

    def valid_time
      if start_time == end_time
        // Error...
      end
      if start_time > end_time
        // Error...
      end
    end

Time zone in application.rb:

config.time_zone = 'Pacific Time (US & Canada)'

Aucun commentaire:

Enregistrer un commentaire