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