aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/application/initializers/hooks_test.rb
blob: 198abbe861d15939f3984164098f389768dc1d00 (plain) (blame)
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
86
require "isolation/abstract_unit"

module ApplicationTests
  class InitializersTest < Test::Unit::TestCase
    include ActiveSupport::Testing::Isolation

    def setup
      build_app
      boot_rails
      FileUtils.rm_rf "#{app_path}/config/environments"
    end

    test "load initializers" do
      app_file "config/initializers/foo.rb", "$foo = true"
      require "#{app_path}/config/environment"
      assert $foo
    end

    test "hooks block works correctly without cache classes (before_eager_load is not called)" do
      add_to_config <<-RUBY
        $initialization_callbacks = []
        config.root = "#{app_path}"
        config.cache_classes = false
        config.before_configuration { $initialization_callbacks << 1 }
        config.before_initialize    { $initialization_callbacks << 2 }
        config.before_eager_load    { Boom }
        config.after_initialize     { $initialization_callbacks << 3 }
      RUBY

      require "#{app_path}/config/environment"
      assert_equal [1,2,3], $initialization_callbacks
    end

    test "hooks block works correctly with cache classes" do
      add_to_config <<-RUBY
        $initialization_callbacks = []
        config.root = "#{app_path}"
        config.cache_classes = true
        config.before_configuration { $initialization_callbacks << 1 }
        config.before_initialize    { $initialization_callbacks << 2 }
        config.before_eager_load    { $initialization_callbacks << 3 }
        config.after_initialize     { $initialization_callbacks << 4 }
      RUBY

      require "#{app_path}/config/environment"
      assert_equal [1,2,3,4], $initialization_callbacks
    end

    test "after_initialize runs after frameworks have been initialized" do
      $activerecord_configurations = nil
      add_to_config <<-RUBY
        config.after_initialize { $activerecord_configurations = ActiveRecord::Base.configurations }
      RUBY

      require "#{app_path}/config/environment"
      assert $activerecord_configurations
      assert $activerecord_configurations['development']
    end

    test "after_initialize happens after to_prepare in development" do
      $order = []
      add_to_config <<-RUBY
        config.cache_classes = false
        config.after_initialize { $order << :after_initialize }
        config.to_prepare { $order << :to_prepare }
      RUBY

      require "#{app_path}/config/environment"
      assert_equal [:to_prepare, :after_initialize], $order
    end

    test "after_initialize happens after to_prepare in production" do
      $order = []
      add_to_config <<-RUBY
        config.cache_classes = true
        config.after_initialize { $order << :after_initialize }
        config.to_prepare { $order << :to_prepare }
      RUBY

      require "#{app_path}/config/application"
      Rails.env.replace "production"
      require "#{app_path}/config/environment"
      assert_equal [:to_prepare, :after_initialize], $order
    end
  end
end