aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/application/initializers/initializers_test.rb
blob: 0c3de7ce33faac75b6902cf3e8221ebf3ec3a920 (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
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 "after_initialize block works correctly" do
      add_to_config <<-RUBY
        config.root = "#{app_path}"
        config.after_initialize { $test_after_initialize_block1 = "success" }
        config.after_initialize { $test_after_initialize_block2 = "congratulations" }
      RUBY
      require "#{app_path}/config/environment"

      assert_equal "success", $test_after_initialize_block1
      assert_equal "congratulations", $test_after_initialize_block2
    end

    test "after_initialize block works correctly when no block is passed" do
      add_to_config <<-RUBY
        config.root = "#{app_path}"
        config.after_initialize { $test_after_initialize_block1 = "success" }
        config.after_initialize # don't pass a block, this is what we're testing!
        config.after_initialize { $test_after_initialize_block2 = "congratulations" }
      RUBY
      require "#{app_path}/config/environment"

      assert_equal "success", $test_after_initialize_block1
      assert_equal "congratulations", $test_after_initialize_block2
    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
  end
end