Permalink
Browse files

Add more tests to some key points in Railties.

Signed-off-by: Carl Lerche <carllerche@mac.com>
  • Loading branch information...
1 parent 64ea3df commit e7418ab63cc4aa024367851f2f692032840cfe76 @josevalim josevalim committed with Carl Lerche Jan 27, 2010
View
4 railties/lib/rails/application.rb
@@ -31,6 +31,10 @@ def inherited(base)
Rails.application = base.instance
end
+ def respond_to?(*args)
+ super || instance.respond_to?(*args)
+ end
+
protected
def method_missing(*args, &block)
View
11 railties/test/application/configuration_test.rb
@@ -17,19 +17,28 @@ def app
end
def setup
- FileUtils.rm_rf(new_app) if File.directory?(new_app)
build_app
boot_rails
FileUtils.rm_rf("#{app_path}/config/environments")
end
+ def teardown
+ FileUtils.rm_rf(new_app) if File.directory?(new_app)
+ end
+
test "Rails::Application.instance is nil until app is initialized" do
require 'rails'
assert_nil Rails::Application.instance
require "#{app_path}/config/environment"
assert_equal AppTemplate::Application.instance, Rails::Application.instance
end
+ test "Rails::Application responds to all instance methods" do
+ require "#{app_path}/config/environment"
+ assert_respond_to Rails::Application, :routes_reloader
+ assert_equal Rails::Application.routes_reloader, Rails.application.routes_reloader
+ end
+
test "the application root is set correctly" do
require "#{app_path}/config/environment"
assert_equal Pathname.new(app_path), Rails.application.root
View
6 railties/test/application/initializers/frameworks_test.rb
@@ -71,6 +71,12 @@ def setup
assert_equal expects, middleware & expects
end
+ test "active_record extensions are applied to ActiveRecord" do
+ add_to_config "config.active_record.table_name_prefix = 'tbl_'"
+ require "#{app_path}/config/environment"
+ assert_equal 'tbl_', ActiveRecord::Base.table_name_prefix
+ end
+
test "database middleware doesn't initialize when activerecord is not in frameworks" do
use_frameworks []
require "#{app_path}/config/environment"
View
16 railties/test/application/initializers/load_path_test.rb
@@ -10,7 +10,6 @@ def setup
FileUtils.rm_rf "#{app_path}/config/environments"
end
- # General
test "initializing an application adds the application paths to the load path" do
add_to_config <<-RUBY
config.root = "#{app_path}"
@@ -20,10 +19,24 @@ def setup
assert $:.include?("#{app_path}/app/models")
end
+ test "initializing an application eager load any path under app" do
+ app_file "app/anything/foo.rb", <<-RUBY
+ module Foo; end
+ RUBY
+
+ add_to_config <<-RUBY
+ config.root = "#{app_path}"
+ RUBY
+
+ require "#{app_path}/config/environment"
+ assert Foo
+ end
+
test "eager loading loads parent classes before children" do
app_file "lib/zoo.rb", <<-ZOO
class Zoo ; include ReptileHouse ; end
ZOO
+
app_file "lib/zoo/reptile_house.rb", <<-ZOO
module Zoo::ReptileHouse ; end
ZOO
@@ -34,7 +47,6 @@ module Zoo::ReptileHouse ; end
RUBY
require "#{app_path}/config/environment"
-
assert Zoo
end
View
45 railties/test/railties/configuration_test.rb
@@ -1,45 +0,0 @@
-require "isolation/abstract_unit"
-
-module RailtiesTest
- class ConfigurationTest < Test::Unit::TestCase
- def setup
- build_app
- boot_rails
- require "rails/all"
- end
-
- test "config is available to plugins" do
- class Foo < Rails::Railtie ; end
- assert_nil Foo.config.action_controller.foo
- end
-
- test "a config name is available for the plugin" do
- class Foo < Rails::Railtie ; config.foo.greetings = "hello" ; end
- assert_equal "hello", Foo.config.foo.greetings
- end
-
- test "railtie configurations are available in the application" do
- class Foo < Rails::Railtie ; config.foo.greetings = "hello" ; end
- require "#{app_path}/config/application"
- assert_equal "hello", AppTemplate::Application.config.foo.greetings
- end
-
- test "railtie config merges are deep" do
- class Foo < Rails::Railtie ; config.foo.greetings = 'hello' ; end
- class Bar < Rails::Railtie
- config.foo.bar = "bar"
- end
- assert_equal "hello", Bar.config.foo.greetings
- assert_equal "bar", Bar.config.foo.bar
- end
-
- test "railtie can add subscribers" do
- begin
- class Foo < Rails::Railtie; subscriber(Rails::Subscriber.new); end
- assert_kind_of Rails::Subscriber, Rails::Subscriber.subscribers[:foo]
- ensure
- Rails::Subscriber.subscribers.clear
- end
- end
- end
-end
View
4 railties/test/railties/engine_test.rb
@@ -23,5 +23,9 @@ class Engine < ::Rails::Engine
def reload_config
:reload_engines
end
+
+ test "Rails::Engine itself does not respond to config" do
+ assert !Rails::Engine.respond_to?(:config)
+ end
end
end
View
82 railties/test/railties/framework_extension_test.rb
@@ -1,82 +0,0 @@
-require "isolation/abstract_unit"
-
-module RailtiesTest
- class FrameworkExtensionTest < Test::Unit::TestCase
- include ActiveSupport::Testing::Isolation
-
- def setup
- build_app
- boot_rails
- FileUtils.rm_rf("#{app_path}/config/environments")
- require "rails/all"
- end
-
- test "rake_tasks block is executed when MyApp.load_tasks is called" do
- $ran_block = false
-
- class MyTie < Rails::Railtie
- rake_tasks do
- $ran_block = true
- end
- end
-
- require "#{app_path}/config/environment"
-
- assert !$ran_block
- require 'rake'
- require 'rake/testtask'
- require 'rake/rdoctask'
-
- AppTemplate::Application.load_tasks
- assert $ran_block
- end
-
- test "generators block is executed when MyApp.load_generators is called" do
- $ran_block = false
-
- class MyTie < Rails::Railtie
- generators do
- $ran_block = true
- end
- end
-
- require "#{app_path}/config/environment"
-
- assert !$ran_block
- AppTemplate::Application.load_generators
- assert $ran_block
- end
-
- test "railtie initializer" do
- $ran_block = false
-
- class MyTie < Rails::Railtie
- initializer :something_nice do
- $ran_block = true
- end
- end
-
- assert !$ran_block
- require "#{app_path}/config/environment"
- assert $ran_block
- end
- end
-
- class ActiveRecordExtensionTest < Test::Unit::TestCase
- include ActiveSupport::Testing::Isolation
-
- def setup
- build_app
- boot_rails
- FileUtils.rm_rf("#{app_path}/config/environments")
- end
-
- test "active_record extensions are applied to ActiveRecord" do
- add_to_config "config.active_record.table_name_prefix = 'tbl_'"
-
- require "#{app_path}/config/environment"
-
- assert_equal 'tbl_', ActiveRecord::Base.table_name_prefix
- end
- end
-end
View
11 railties/test/railties/plugin_test.rb
@@ -19,6 +19,17 @@ def reload_config
:reload_plugins
end
+ test "Rails::Plugin itself does not respond to config" do
+ assert !Rails::Plugin.respond_to?(:config)
+ end
+
+ test "cannot inherit from Rails::Plugin" do
+ boot_rails
+ assert_raise RuntimeError do
+ class Foo < Rails::Plugin; end
+ end
+ end
+
test "plugin can load the file with the same name in lib" do
boot_rails
require "bukkits"
View
132 railties/test/railties/railtie_test.rb
@@ -0,0 +1,132 @@
+require "isolation/abstract_unit"
+
+module RailtiesTest
+ class RailtieTest < Test::Unit::TestCase
+ include ActiveSupport::Testing::Isolation
+
+ def setup
+ build_app
+ boot_rails
+ FileUtils.rm_rf("#{app_path}/config/environments")
+ require "rails/all"
+ end
+
+ def app
+ @app ||= Rails.application
+ end
+
+ test "Rails::Railtie itself does not respond to config" do
+ assert !Rails::Railtie.respond_to?(:config)
+ end
+
+ test "cannot inherit from a railtie" do
+ class Foo < Rails::Railtie ; end
+ assert_raise RuntimeError do
+ class Bar < Foo; end
+ end
+ end
+
+ test "config is available to railtie" do
+ class Foo < Rails::Railtie ; end
+ assert_nil Foo.config.action_controller.foo
+ end
+
+ test "config name is available for the railtie" do
+ class Foo < Rails::Railtie ; config.foo.greetings = "hello" ; end
+ assert_equal "hello", Foo.config.foo.greetings
+ end
+
+ test "railtie configurations are available in the application" do
+ class Foo < Rails::Railtie ; config.foo.greetings = "hello" ; end
+ require "#{app_path}/config/application"
+ assert_equal "hello", AppTemplate::Application.config.foo.greetings
+ end
+
+ test "railtie config merges are deep" do
+ class Foo < Rails::Railtie ; config.foo.greetings = 'hello' ; end
+ class Bar < Rails::Railtie
+ config.foo.bar = "bar"
+ end
+ assert_equal "hello", Bar.config.foo.greetings
+ assert_equal "bar", Bar.config.foo.bar
+ end
+
+ test "railtie can add subscribers" do
+ begin
+ class Foo < Rails::Railtie ; subscriber(Rails::Subscriber.new) ; end
+ assert_kind_of Rails::Subscriber, Rails::Subscriber.subscribers[:foo]
+ ensure
+ Rails::Subscriber.subscribers.clear
+ end
+ end
+
+ test "railtie can add to_prepare callbacks" do
+ $to_prepare = false
+ class Foo < Rails::Railtie ; config.to_prepare { $to_prepare = true } ; end
+ assert !$to_prepare
+ require "#{app_path}/config/environment"
+ require "rack/test"
+ extend Rack::Test::Methods
+ get "/"
+ assert $to_prepare
+ end
+
+ test "railtie can add after_initialize callbacks" do
+ $after_initialize = false
+ class Foo < Rails::Railtie ; config.after_initialize { $after_initialize = true } ; end
+ assert !$after_initialize
+ require "#{app_path}/config/environment"
+ assert $after_initialize
+ end
+
+ test "rake_tasks block is executed when MyApp.load_tasks is called" do
+ $ran_block = false
+
+ class MyTie < Rails::Railtie
+ rake_tasks do
+ $ran_block = true
+ end
+ end
+
+ require "#{app_path}/config/environment"
+
+ assert !$ran_block
+ require 'rake'
+ require 'rake/testtask'
+ require 'rake/rdoctask'
+
+ AppTemplate::Application.load_tasks
+ assert $ran_block
+ end
+
+ test "generators block is executed when MyApp.load_generators is called" do
+ $ran_block = false
+
+ class MyTie < Rails::Railtie
+ generators do
+ $ran_block = true
+ end
+ end
+
+ require "#{app_path}/config/environment"
+
+ assert !$ran_block
+ AppTemplate::Application.load_generators
+ assert $ran_block
+ end
+
+ test "railtie can add initializers" do
+ $ran_block = false
+
+ class MyTie < Rails::Railtie
+ initializer :something_nice do
+ $ran_block = true
+ end
+ end
+
+ assert !$ran_block
+ require "#{app_path}/config/environment"
+ assert $ran_block
+ end
+ end
+end
View
9 railties/test/railties/shared_tests.rb
@@ -116,6 +116,15 @@ def bukkits
assert_equal "Hello bukkits\n", response[2].body
end
+ def test_plugin_eager_load_any_path_under_app
+ @plugin.write "app/anything/foo.rb", <<-RUBY
+ module Foo; end
+ RUBY
+
+ boot_rails
+ assert Foo
+ end
+
def test_routes_are_added_to_router
@plugin.write "config/routes.rb", <<-RUBY
class Sprokkit

0 comments on commit e7418ab

Please sign in to comment.