Skip to content

テストヘルパー

spec/support/authentication_helpers.rb
module AuthenticationHelpers
def sign_in(user)
post '/api/auth/login', params: {
email: user.email,
password: user.password
}
@auth_token = JSON.parse(response.body)['token']
end
def auth_headers
{ 'Authorization' => "Bearer #{@auth_token}" }
end
end
RSpec.configure do |config|
config.include AuthenticationHelpers, type: :request
end
spec/support/database_cleaner.rb
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
end
spec/support/time_helpers.rb
RSpec.configure do |config|
config.include ActiveSupport::Testing::TimeHelpers
end
# 使用例
it '時間を固定できること' do
travel_to Time.zone.parse('2024-01-01 12:00:00') do
expect(Time.current).to eq(Time.zone.parse('2024-01-01 12:00:00'))
end
end

「railsは、開発者としての成長に欠かせない『わざ』である——継続的に学び、実践することで真の力となる。」

技術の習得は、ポケモンのレベルアップと同じ——一歩ずつ、着実に成長していくことが重要です。railsをマスターし、より強力な開発者を目指しましょう。