テストヘルパー
テストヘルパー
Section titled “テストヘルパー”認証ヘルパー
Section titled “認証ヘルパー”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}" } endend
RSpec.configure do |config| config.include AuthenticationHelpers, type: :requestendデータベースクリーンアップ
Section titled “データベースクリーンアップ”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 endend時間のモック
Section titled “時間のモック”RSpec.configure do |config| config.include ActiveSupport::Testing::TimeHelpersend
# 使用例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')) endend