提交 e7418ab6 编写于 作者: J José Valim 提交者: Carl Lerche

Add more tests to some key points in Railties.

Signed-off-by: NCarl Lerche <carllerche@mac.com>
上级 64ea3dfd
......@@ -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)
......
......@@ -17,12 +17,15 @@ 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
......@@ -30,6 +33,12 @@ def setup
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
......
......@@ -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"
......
......@@ -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
......
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
......@@ -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
......@@ -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"
......
require "isolation/abstract_unit"
module RailtiesTest
class FrameworkExtensionTest < Test::Unit::TestCase
class RailtieTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
......@@ -11,6 +11,74 @@ def setup
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
......@@ -47,7 +115,7 @@ class MyTie < Rails::Railtie
assert $ran_block
end
test "railtie initializer" do
test "railtie can add initializers" do
$ran_block = false
class MyTie < Rails::Railtie
......@@ -61,22 +129,4 @@ class MyTie < Rails::Railtie
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
\ No newline at end of file
end
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册