aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/initializable_test.rb
blob: 7c8aed00c9629c2db3ebd4a3e602c04080a9b804 (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
require 'abstract_unit'
require 'rails/initializable'

module InitializableTests

  class Foo
    extend Rails::Initializable

    class << self
      attr_accessor :foo, :bar
    end

    initializer :omg do
      @foo ||= 0
      @foo += 1
    end
  end

  class Bar < Foo
    initializer :bar do
      @bar ||= 0
      @bar += 1
    end
  end

  module Word
    extend Rails::Initializable

    initializer :word do
      $word = "bird"
    end
  end

  class Basic < ActiveSupport::TestCase
    include ActiveSupport::Testing::Isolation

    test "initializers run" do
      Foo.initializers.run
      assert_equal 1, Foo.foo
    end

    test "initializers are inherited" do
      Bar.initializers.run
      assert_equal [1, 1], [Bar.foo, Bar.bar]
    end

    test "initializers only get run once" do
      Foo.initializers.run
      Foo.initializers.run
      assert_equal 1, Foo.foo
    end

    test "running initializers on children does not effect the parent" do
      Bar.initializers.run
      assert_nil Foo.foo
      assert_nil Foo.bar
    end

    test "inherited initializers are the same objects" do
      assert Foo.initializers[:foo].eql?(Bar.initializers[:foo])
    end

    test "initializing with modules" do
      Word.initializers.run
      assert_equal "bird", $word
    end
  end
end