|
12 | 12 |
|
13 | 13 | context "when a definition file paths exist" do
|
14 | 14 | it "registers a reloader" do
|
15 |
| - allow(reloader_class).to receive(:new) |
| 15 | + file_watcher = file_watcher_double |
16 | 16 |
|
17 |
| - run_reloader(["spec/fixtures/factories", "not_exist_directory"]) |
| 17 | + run_reloader( |
| 18 | + ["spec/fixtures/factories", "not_exist_directory"], |
| 19 | + file_watcher |
| 20 | + ) |
18 | 21 |
|
19 |
| - expect(reloader_class).to have_received(:new) |
| 22 | + expect(file_watcher).to have_received(:new) |
20 | 23 | end
|
21 | 24 | end
|
22 | 25 |
|
23 | 26 | context "when a file exists but not a directory" do
|
24 | 27 | it "registers a reloader" do
|
25 |
| - allow(reloader_class).to receive(:new) |
| 28 | + file_watcher = file_watcher_double |
26 | 29 |
|
27 |
| - run_reloader(["spec/fake_app", "not_exist_directory"]) |
| 30 | + run_reloader( |
| 31 | + ["spec/fake_app", "not_exist_directory"], |
| 32 | + file_watcher |
| 33 | + ) |
28 | 34 |
|
29 |
| - expect(reloader_class).to have_received(:new) |
| 35 | + expect(file_watcher).to have_received(:new) |
30 | 36 | end
|
31 | 37 | end
|
32 | 38 |
|
33 | 39 | context "when a definition file paths NOT exist" do
|
34 | 40 | it "does NOT register a reloader" do
|
35 |
| - allow(reloader_class).to receive(:new) |
| 41 | + file_watcher = file_watcher_double |
36 | 42 |
|
37 |
| - run_reloader(["not_exist_directory"]) |
| 43 | + run_reloader(["not_exist_directory"], file_watcher) |
38 | 44 |
|
39 |
| - expect(reloader_class).not_to have_received(:new) |
| 45 | + expect(file_watcher).not_to have_received(:new) |
40 | 46 | end
|
41 | 47 | end
|
42 | 48 |
|
43 |
| - def run_reloader(definition_file_paths) |
| 49 | + def run_reloader(definition_file_paths, file_watcher) |
44 | 50 | FactoryBot.definition_file_paths = definition_file_paths
|
45 |
| - FactoryBotRails::Reloader |
46 |
| - .new(Rails.application, Rails.application.config).run |
| 51 | + app = app_double(file_watcher) |
| 52 | + FactoryBotRails::Reloader.new(app).run |
47 | 53 | end
|
48 | 54 |
|
49 |
| - def reloader_class |
50 |
| - Rails.application.config.file_watcher |
| 55 | + def file_watcher_double |
| 56 | + class_double( |
| 57 | + Rails.application.config.file_watcher, |
| 58 | + new: double(:reloader, execute: nil) |
| 59 | + ) |
| 60 | + end |
| 61 | + |
| 62 | + def app_double(file_watcher) |
| 63 | + instance_double( |
| 64 | + Rails.application.class, |
| 65 | + config: app_config_double(file_watcher), |
| 66 | + reloader: reloader_double, |
| 67 | + reloaders: [] |
| 68 | + ) |
| 69 | + end |
| 70 | + |
| 71 | + def app_config_double(file_watcher) |
| 72 | + instance_double( |
| 73 | + Rails.application.config.class, |
| 74 | + file_watcher: file_watcher |
| 75 | + ) |
| 76 | + end |
| 77 | + |
| 78 | + def reloader_double |
| 79 | + class_double( |
| 80 | + Rails.application.reloader, |
| 81 | + to_prepare: nil |
| 82 | + ) |
51 | 83 | end
|
52 | 84 | end
|
53 | 85 | end
|
0 commit comments