From 706a43bebd00eae24f3c3ea2a3bf3db383b69561 Mon Sep 17 00:00:00 2001 From: Jun Aruga Date: Mon, 15 Mar 2021 21:28:54 +0100 Subject: [PATCH] Verify the testing database before running tests. (#1174) 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 --- spec/spec_helper.rb | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b0bf598aa..7a2a36196 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 @@ -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