@@ -141,7 +141,6 @@ class DatabaseTasksCreateAllTest < ActiveRecord::TestCase
141
141
def setup
142
142
@configurations = { "development" => { "database" => "my-db" } }
143
143
144
- ActiveRecord ::Base . stubs ( :configurations ) . returns ( @configurations )
145
144
# To refrain from connecting to a newly created empty DB in sqlite3_mem tests
146
145
ActiveRecord ::Base . connection_handler . stubs ( :establish_connection )
147
146
@@ -156,51 +155,71 @@ def teardown
156
155
def test_ignores_configurations_without_databases
157
156
@configurations [ "development" ] . merge! ( "database" => nil )
158
157
159
- assert_not_called ( ActiveRecord ::Tasks ::DatabaseTasks , :create ) do
160
- ActiveRecord ::Tasks ::DatabaseTasks . create_all
158
+ with_stubbed_configurations do
159
+ assert_not_called ( ActiveRecord ::Tasks ::DatabaseTasks , :create ) do
160
+ ActiveRecord ::Tasks ::DatabaseTasks . create_all
161
+ end
161
162
end
162
163
end
163
164
164
165
def test_ignores_remote_databases
165
166
@configurations [ "development" ] . merge! ( "host" => "my.server.tld" )
166
167
167
- assert_not_called ( ActiveRecord ::Tasks ::DatabaseTasks , :create ) do
168
- ActiveRecord ::Tasks ::DatabaseTasks . create_all
168
+ with_stubbed_configurations do
169
+ assert_not_called ( ActiveRecord ::Tasks ::DatabaseTasks , :create ) do
170
+ ActiveRecord ::Tasks ::DatabaseTasks . create_all
171
+ end
169
172
end
170
173
end
171
174
172
175
def test_warning_for_remote_databases
173
176
@configurations [ "development" ] . merge! ( "host" => "my.server.tld" )
174
177
175
- ActiveRecord ::Tasks ::DatabaseTasks . create_all
178
+ with_stubbed_configurations do
179
+ ActiveRecord ::Tasks ::DatabaseTasks . create_all
176
180
177
- assert_match "This task only modifies local databases. my-db is on a remote host." ,
178
- $stderr. string
181
+ assert_match "This task only modifies local databases. my-db is on a remote host." ,
182
+ $stderr. string
183
+ end
179
184
end
180
185
181
186
def test_creates_configurations_with_local_ip
182
187
@configurations [ "development" ] . merge! ( "host" => "127.0.0.1" )
183
188
184
- assert_called ( ActiveRecord ::Tasks ::DatabaseTasks , :create ) do
185
- ActiveRecord ::Tasks ::DatabaseTasks . create_all
189
+ with_stubbed_configurations do
190
+ assert_called ( ActiveRecord ::Tasks ::DatabaseTasks , :create ) do
191
+ ActiveRecord ::Tasks ::DatabaseTasks . create_all
192
+ end
186
193
end
187
194
end
188
195
189
196
def test_creates_configurations_with_local_host
190
197
@configurations [ "development" ] . merge! ( "host" => "localhost" )
191
198
192
- assert_called ( ActiveRecord ::Tasks ::DatabaseTasks , :create ) do
193
- ActiveRecord ::Tasks ::DatabaseTasks . create_all
199
+ with_stubbed_configurations do
200
+ assert_called ( ActiveRecord ::Tasks ::DatabaseTasks , :create ) do
201
+ ActiveRecord ::Tasks ::DatabaseTasks . create_all
202
+ end
194
203
end
195
204
end
196
205
197
206
def test_creates_configurations_with_blank_hosts
198
207
@configurations [ "development" ] . merge! ( "host" => nil )
199
208
200
- assert_called ( ActiveRecord ::Tasks ::DatabaseTasks , :create ) do
201
- ActiveRecord ::Tasks ::DatabaseTasks . create_all
209
+ with_stubbed_configurations do
210
+ assert_called ( ActiveRecord ::Tasks ::DatabaseTasks , :create ) do
211
+ ActiveRecord ::Tasks ::DatabaseTasks . create_all
212
+ end
202
213
end
203
214
end
215
+
216
+ private
217
+
218
+ def with_stubbed_configurations
219
+ ActiveRecord ::Base . stub ( :configurations , @configurations ) do
220
+ yield
221
+ end
222
+ end
204
223
end
205
224
206
225
class DatabaseTasksCreateCurrentTest < ActiveRecord ::TestCase
0 commit comments