Skip to content

Commit

Permalink
Verify the testing database before running tests. (#1174)
Browse files Browse the repository at this point in the history
To avoid seeing a lot of test failures on the database connection error,
and guide the user how to fix.

Sort the before after blocks in the following actually called order.
See https://relishapp.com/rspec/rspec-core/v/2-99/docs/hooks/before-and-after-hooks
  • Loading branch information
junaruga authored Mar 15, 2021
1 parent 692adc6 commit 706a43b
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,21 @@ def clock_time
end
end

config.before :each do
@client = new_client
end

config.after :each do
@clients.each(&:close)
config.before(:suite) do
begin
new_client
rescue Mysql2::Error => e
username = DatabaseCredentials['root']['username']
database = DatabaseCredentials['root']['database']
message = %(
An error occurred while connecting to the testing database server.
Make sure that the database server is running.
Make sure that `mysql -u #{username} [options] #{database}` succeeds by the root user config in spec/configuration.yml.
Make sure that the testing database '#{database}' exists. If it does not exist, create it.
)
warn message
raise e
end
end

config.before(:all) do
Expand Down Expand Up @@ -126,4 +135,12 @@ def clock_time
]
end
end

config.before(:each) do
@client = new_client
end

config.after(:each) do
@clients.each(&:close)
end
end

0 comments on commit 706a43b

Please sign in to comment.