Skip to content

Instantly share code, notes, and snippets.

@lujanfernaud
Last active April 20, 2021 00:39
Show Gist options
  • Save lujanfernaud/6fff63d980c994ce8a3d355a28830eda to your computer and use it in GitHub Desktop.
Save lujanfernaud/6fff63d980c994ce8a3d355a28830eda to your computer and use it in GitHub Desktop.
Rails: Test datetime_select using Capybara

Rails: Test datetime_select using Capybara

# test_helper.rb

def select_date_and_time(date, **options)
  return nil unless date
  
  field = options[:from]
  select date.strftime("%Y"),  from: "#{field}_1i" # Year.
  select date.strftime("%B"),  from: "#{field}_2i" # Month.
  select date.strftime("%-d"), from: "#{field}_3i" # Day.
  select date.strftime("%H"),  from: "#{field}_4i" # Hour.
  select date.strftime("%M"),  from: "#{field}_5i" # Minutes.
end
# events_creation_test.rb

test "create event with valid data" do
  ...
  select_date_and_time @event.start_date, from: "event_start_date"
  select_date_and_time @event.end_date,   from: "event_end_date"
  ...
end

Original idea: https://stackoverflow.com/a/27244201/6212572
Ruby #strftime: https://ruby-doc.org/core-2.2.0/Time.html#method-i-strftime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment