Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / railties / test / initializer_test.rb
blob156f670e87c41abf5d6e8b26cbaebadc2bccb922
1 require "#{File.dirname(__FILE__)}/abstract_unit"
2 require 'initializer'
4 class ConfigurationMock < Rails::Configuration
5   attr_reader :environment_path
7   def initialize(envpath)
8     super()
9     @environment_path = envpath
10   end
11 end
13 class Initializer_load_environment_Test < Test::Unit::TestCase
15   def test_load_environment_with_constant
16     config = ConfigurationMock.new("#{File.dirname(__FILE__)}/fixtures/environment_with_constant.rb")
17     assert_nil $initialize_test_set_from_env
18     Rails::Initializer.run(:load_environment, config)
19     assert_equal "success", $initialize_test_set_from_env
20   ensure
21     $initialize_test_set_from_env = nil
22   end
24 end
26 class Initializer_after_initialize_with_blocks_environment_Test < Test::Unit::TestCase
27   def setup
28     config = ConfigurationMock.new("")
29     config.after_initialize do
30       $test_after_initialize_block1 = "success"
31     end
32     config.after_initialize do
33       $test_after_initialize_block2 = "congratulations"
34     end    
35     assert_nil $test_after_initialize_block1
36     assert_nil $test_after_initialize_block2    
38     Rails::Initializer.run(:after_initialize, config)
39   end
40   
41   def teardown
42     $test_after_initialize_block1 = nil
43     $test_after_initialize_block2 = nil    
44   end
46   def test_should_have_called_the_first_after_initialize_block
47     assert_equal "success", $test_after_initialize_block1
48   end
49   
50   def test_should_have_called_the_second_after_initialize_block
51     assert_equal "congratulations", $test_after_initialize_block2
52   end
53 end
54   
55 class Initializer_after_initialize_with_no_block_environment_Test < Test::Unit::TestCase
57   def setup
58     config = ConfigurationMock.new("")
59     config.after_initialize do
60       $test_after_initialize_block1 = "success"
61     end
62     config.after_initialize # don't pass a block, this is what we're testing!
63     config.after_initialize do
64       $test_after_initialize_block2 = "congratulations"
65     end    
66     assert_nil $test_after_initialize_block1
68     Rails::Initializer.run(:after_initialize, config)
69   end
71   def teardown
72     $test_after_initialize_block1 = nil
73     $test_after_initialize_block2 = nil    
74   end
76   def test_should_have_called_the_first_after_initialize_block
77     assert_equal "success", $test_after_initialize_block1, "should still get set"
78   end
80   def test_should_have_called_the_second_after_initialize_block
81     assert_equal "congratulations", $test_after_initialize_block2
82   end
84 end
86 uses_mocha 'framework paths' do
87   class ConfigurationFrameworkPathsTests < Test::Unit::TestCase
88     def setup
89       @config = Rails::Configuration.new
90       @config.frameworks.clear
91       
92       File.stubs(:directory?).returns(true)
93       @config.stubs(:framework_root_path).returns('')
94     end
96     def test_minimal
97       expected = %w(
98         /railties
99         /railties/lib
100         /activesupport/lib
101       )
102       assert_equal expected, @config.framework_paths
103     end
105     def test_actioncontroller_or_actionview_add_actionpack
106       @config.frameworks << :action_controller
107       assert_framework_path '/actionpack/lib'
108       
109       @config.frameworks = [:action_view]
110       assert_framework_path '/actionpack/lib'
111     end
113     def test_paths_for_ar_ares_and_mailer
114       [:active_record, :action_mailer, :active_resource, :action_web_service].each do |framework|
115         @config.frameworks = [framework]
116         assert_framework_path "/#{framework.to_s.gsub('_', '')}/lib"
117       end
118     end
120     def test_unknown_framework_raises_error
121       @config.frameworks << :action_foo
122       initializer = Rails::Initializer.new @config
123       initializer.expects(:require).raises(LoadError)
125       assert_raise RuntimeError do
126         initializer.send :require_frameworks
127       end
128     end
130     protected
132       def assert_framework_path(path)
133         assert @config.framework_paths.include?(path),
134           "<#{path.inspect}> not found among <#{@config.framework_paths.inspect}>"
135       end
136   end
139 uses_mocha "Initializer plugin loading tests" do
140   require File.dirname(__FILE__) + '/plugin_test_helper'
142   class InitializerPluginLoadingTests < Test::Unit::TestCase
143     def setup
144       @configuration     = Rails::Configuration.new
145       @configuration.plugin_paths << plugin_fixture_root_path
146       @initializer       = Rails::Initializer.new(@configuration)
147       @valid_plugin_path = plugin_fixture_path('default/stubby')
148       @empty_plugin_path = plugin_fixture_path('default/empty')
149     end
151     def test_no_plugins_are_loaded_if_the_configuration_has_an_empty_plugin_list
152       only_load_the_following_plugins! []
153       @initializer.load_plugins
154       assert_equal [], @initializer.loaded_plugins
155     end
157     def test_only_the_specified_plugins_are_located_in_the_order_listed
158       plugin_names = [:plugin_with_no_lib_dir, :acts_as_chunky_bacon]
159       only_load_the_following_plugins! plugin_names
160       load_plugins!
161       assert_plugins plugin_names, @initializer.loaded_plugins
162     end
164     def test_all_plugins_are_loaded_when_registered_plugin_list_is_untouched
165       failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
166       load_plugins!
167       assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @initializer.loaded_plugins, failure_tip
168     end
170     def test_all_plugins_loaded_when_all_is_used
171       plugin_names = [:stubby, :acts_as_chunky_bacon, :all]
172       only_load_the_following_plugins! plugin_names
173       load_plugins!
174       failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
175       assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :plugin_with_no_lib_dir], @initializer.loaded_plugins, failure_tip
176     end
178     def test_all_plugins_loaded_after_all
179       plugin_names = [:stubby, :all, :acts_as_chunky_bacon]
180       only_load_the_following_plugins! plugin_names
181       load_plugins!
182       failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
183       assert_plugins [:stubby, :a, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @initializer.loaded_plugins, failure_tip
184     end
186     def test_plugin_names_may_be_strings
187       plugin_names = ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir]
188       only_load_the_following_plugins! plugin_names
189       load_plugins!
190       failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
191       assert_plugins plugin_names, @initializer.loaded_plugins, failure_tip
192     end
194     def test_registering_a_plugin_name_that_does_not_exist_raises_a_load_error
195       only_load_the_following_plugins! [:stubby, :acts_as_a_non_existant_plugin]
196       assert_raises(LoadError) do
197         load_plugins!
198       end
199     end
200     
201     def test_should_ensure_all_loaded_plugins_load_paths_are_added_to_the_load_path
202       only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
204       @initializer.add_plugin_load_paths
205       
206       assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/stubby'), 'lib'))
207       assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib'))
208     end
209     
210     private
211     
212       def load_plugins!
213         @initializer.add_plugin_load_paths
214         @initializer.load_plugins
215       end
216   end
217   
218 end