From 67fe1e6a5888dbf56c8600e934b81e5870299d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arist=C3=B3teles=20Coutinho?= Date: Tue, 30 Jan 2024 12:15:19 -0300 Subject: [PATCH] fix: environment database --- .github/workflows/test.yaml | 2 +- app.rb | 4 +++- spec/app_spec.rb | 11 +++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 2964053..12e6660 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -32,5 +32,5 @@ jobs: run: RACK_ENV='test' bundle exec ruby db/setup.rb - name: Run tests - run: bundle exec rspec + run: RACK_ENV='test' bundle exec rspec diff --git a/app.rb b/app.rb index 7d6530c..96f93d2 100644 --- a/app.rb +++ b/app.rb @@ -78,7 +78,9 @@ def render(view) # DATABASE CONNECTION # - def database = @database ||= SQLite3::Database.new('db/teachers.db') + def database + @database ||= SQLite3::Database.new ENV['RACK_ENV'] == 'test' ? 'db/teachers_test.db' : 'db/teachers.db' + end # SQL QUERIES # diff --git a/spec/app_spec.rb b/spec/app_spec.rb index 17e6f6e..a79b357 100644 --- a/spec/app_spec.rb +++ b/spec/app_spec.rb @@ -5,6 +5,17 @@ ::RSpec.describe('RatingApp') do describe 'requests' do context 'GET /' do + before do + @database = SQLite3::Database.new('db/teachers_test.db') + @database.execute('INSERT INTO teachers (name) VALUES ("Sócrates"), ("Aristóteles"), ("Epicteto"), ("João")') + end + + after do + @database.execute('DELETE FROM ratings') + + @database.close + end + it 'returns 200' do get '/' expect(last_response).to be_ok