Skip to content

Commit 706a43b

Browse files
authored
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
1 parent 692adc6 commit 706a43b

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

spec/spec_helper.rb

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,21 @@ def clock_time
5757
end
5858
end
5959

60-
config.before :each do
61-
@client = new_client
62-
end
63-
64-
config.after :each do
65-
@clients.each(&:close)
60+
config.before(:suite) do
61+
begin
62+
new_client
63+
rescue Mysql2::Error => e
64+
username = DatabaseCredentials['root']['username']
65+
database = DatabaseCredentials['root']['database']
66+
message = %(
67+
An error occurred while connecting to the testing database server.
68+
Make sure that the database server is running.
69+
Make sure that `mysql -u #{username} [options] #{database}` succeeds by the root user config in spec/configuration.yml.
70+
Make sure that the testing database '#{database}' exists. If it does not exist, create it.
71+
)
72+
warn message
73+
raise e
74+
end
6675
end
6776

6877
config.before(:all) do
@@ -126,4 +135,12 @@ def clock_time
126135
]
127136
end
128137
end
138+
139+
config.before(:each) do
140+
@client = new_client
141+
end
142+
143+
config.after(:each) do
144+
@clients.each(&:close)
145+
end
129146
end

0 commit comments

Comments
 (0)