Rails: Test datetime_select using Capybara
# test_helper.rb
def select_date_and_time(date, **options)
return nil unless date
field = options[:from]
inherit_from: ../.rubocop.yml | |
# Checks if uses of quotes match the configured preference. | |
Style/StringLiterals: | |
Enabled: false | |
# test_helper.rb
def select_date_and_time(date, **options)
return nil unless date
field = options[:from]
# We need to use 'remote_<object>_url' as attribute instead of '<object>'. | |
# Having Image, we would use 'remote_image_url: <url>'. | |
titles = [Faker::RockBand.name, Faker::BossaNova.artist, Faker::Book.title] | |
54.times do |n| | |
start_date = Faker::Date.between(1.day.from_now, 6.months.from_now) | |
end_date = start_date + 1.day | |
Event.create!(title: titles.sample + " ##{n}", |
We need to use File.open(Rails.root.join("<image_path>"))
.
File.open(Rails.root.join("test/fixtures/files/sample.jpeg"))
We find all roles assigned to the user in the resource, a group in this case.
@user.roles.where(resource: @group)
=> [#<Role:0x00000004a80580
id: 24,
This is what we need to do:
export RAILS_ENV=production
rake db:reset
rake assets:precompile
RAILS_SERVE_STATIC_FILES=true
SECRET_KEY_BASE=production rails s
sample_input = "<div>Hi </div>"
parsed_input = Nokogiri::HTML.parse(sample_input).text #=> "Hi "
parsed_input.gsub(/\p{Space}*\z/, "") #=> "Hi"
\p{Space}
catches any whitespace character.
Add reference:
# add_reference :table, :column_name, foreign_key: boolean
add_reference :bookings, :flight, foreign_key: true
Add index:
def random_date_in_period(period)
rand(period).seconds.ago
end
random_date_in_period 1.year #=> Tue, 22 May 2018 04:55:54 UTC +00:00
random_date_in_period 1.year #=> Mon, 06 Nov 2017 19:50:07 UTC +00:00