File: reloader_spec.rb

package info (click to toggle)
ruby-factory-bot-rails 6.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 380 kB
  • sloc: ruby: 645; makefile: 6; sh: 4
file content (85 lines) | stat: -rw-r--r-- 2,060 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# frozen_string_literal: true

describe FactoryBotRails::Reloader do
  describe "#run" do
    before do
      @original_definition_file_paths = FactoryBot.definition_file_paths
    end

    after do
      FactoryBot.definition_file_paths = @original_definition_file_paths
    end

    context "when a definition file paths exist" do
      it "registers a reloader" do
        file_watcher = file_watcher_double

        run_reloader(
          ["spec/fixtures/factories", "not_exist_directory"],
          file_watcher
        )

        expect(file_watcher).to have_received(:new)
      end
    end

    context "when a file exists but not a directory" do
      it "registers a reloader" do
        file_watcher = file_watcher_double

        run_reloader(
          ["spec/fake_app", "not_exist_directory"],
          file_watcher
        )

        expect(file_watcher).to have_received(:new)
      end
    end

    context "when a definition file paths NOT exist" do
      it "does NOT register a reloader" do
        file_watcher = file_watcher_double

        run_reloader(["not_exist_directory"], file_watcher)

        expect(file_watcher).not_to have_received(:new)
      end
    end

    def run_reloader(definition_file_paths, file_watcher)
      FactoryBot.definition_file_paths = definition_file_paths
      app = app_double(file_watcher)
      FactoryBotRails::Reloader.new(app).run
    end

    def file_watcher_double
      class_double(
        Rails.application.config.file_watcher,
        new: double(:reloader, execute: nil)
      )
    end

    def app_double(file_watcher)
      instance_double(
        Rails.application.class,
        config: app_config_double(file_watcher),
        reloader: reloader_double,
        reloaders: []
      )
    end

    def app_config_double(file_watcher)
      instance_double(
        Rails.application.config.class,
        file_watcher: file_watcher
      )
    end

    def reloader_double
      class_double(
        Rails.application.reloader,
        to_prepare: nil
      )
    end
  end
end