PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/railties/test/railties/railtie_test.rb

https://bitbucket.org/druly/rails_cherry_pick
Ruby | 191 lines | 155 code | 36 blank | 0 comment | 2 complexity | 065336c43196bc03775f6ab67146aabc MD5 | raw file
  1. require "isolation/abstract_unit"
  2. module RailtiesTest
  3. class RailtieTest < Test::Unit::TestCase
  4. include ActiveSupport::Testing::Isolation
  5. def setup
  6. build_app
  7. boot_rails
  8. FileUtils.rm_rf("#{app_path}/config/environments")
  9. require "rails/all"
  10. end
  11. def teardown
  12. teardown_app
  13. end
  14. def app
  15. @app ||= Rails.application
  16. end
  17. test "Rails::Railtie itself does not respond to config" do
  18. assert !Rails::Railtie.respond_to?(:config)
  19. end
  20. test "Railtie provides railtie_name" do
  21. begin
  22. class ::FooBarBaz < Rails::Railtie ; end
  23. assert_equal "foo_bar_baz", ::FooBarBaz.railtie_name
  24. ensure
  25. Object.send(:remove_const, :"FooBarBaz")
  26. end
  27. end
  28. test "railtie_name can be set manualy" do
  29. class Foo < Rails::Railtie
  30. railtie_name "bar"
  31. end
  32. assert_equal "bar", Foo.railtie_name
  33. end
  34. test "cannot inherit from a railtie" do
  35. class Foo < Rails::Railtie ; end
  36. assert_raise RuntimeError do
  37. class Bar < Foo; end
  38. end
  39. end
  40. test "config is available to railtie" do
  41. class Foo < Rails::Railtie ; end
  42. assert_nil Foo.config.action_controller.foo
  43. end
  44. test "config name is available for the railtie" do
  45. class Foo < Rails::Railtie
  46. config.foo = ActiveSupport::OrderedOptions.new
  47. config.foo.greetings = "hello"
  48. end
  49. assert_equal "hello", Foo.config.foo.greetings
  50. end
  51. test "railtie configurations are available in the application" do
  52. class Foo < Rails::Railtie
  53. config.foo = ActiveSupport::OrderedOptions.new
  54. config.foo.greetings = "hello"
  55. end
  56. require "#{app_path}/config/application"
  57. assert_equal "hello", AppTemplate::Application.config.foo.greetings
  58. end
  59. test "railtie can add to_prepare callbacks" do
  60. $to_prepare = false
  61. class Foo < Rails::Railtie ; config.to_prepare { $to_prepare = true } ; end
  62. assert !$to_prepare
  63. require "#{app_path}/config/environment"
  64. require "rack/test"
  65. extend Rack::Test::Methods
  66. get "/"
  67. assert $to_prepare
  68. end
  69. test "railtie can add after_initialize callbacks" do
  70. $after_initialize = false
  71. class Foo < Rails::Railtie ; config.after_initialize { $after_initialize = true } ; end
  72. assert !$after_initialize
  73. require "#{app_path}/config/environment"
  74. assert $after_initialize
  75. end
  76. test "rake_tasks block is executed when MyApp.load_tasks is called" do
  77. $ran_block = false
  78. class MyTie < Rails::Railtie
  79. rake_tasks do
  80. $ran_block = true
  81. end
  82. end
  83. require "#{app_path}/config/environment"
  84. assert !$ran_block
  85. require 'rake'
  86. require 'rake/testtask'
  87. require 'rdoc/task'
  88. AppTemplate::Application.load_tasks
  89. assert $ran_block
  90. end
  91. test "rake_tasks block defined in superclass of railtie is also executed" do
  92. $ran_block = []
  93. class Rails::Railtie
  94. rake_tasks do
  95. $ran_block << railtie_name
  96. end
  97. end
  98. class MyTie < Rails::Railtie
  99. railtie_name "my_tie"
  100. end
  101. require "#{app_path}/config/environment"
  102. assert_equal [], $ran_block
  103. require 'rake'
  104. require 'rake/testtask'
  105. require 'rdoc/task'
  106. AppTemplate::Application.load_tasks
  107. assert $ran_block.include?("my_tie")
  108. end
  109. test "generators block is executed when MyApp.load_generators is called" do
  110. $ran_block = false
  111. class MyTie < Rails::Railtie
  112. generators do
  113. $ran_block = true
  114. end
  115. end
  116. require "#{app_path}/config/environment"
  117. assert !$ran_block
  118. AppTemplate::Application.load_generators
  119. assert $ran_block
  120. end
  121. test "console block is executed when MyApp.load_console is called" do
  122. $ran_block = false
  123. class MyTie < Rails::Railtie
  124. console do
  125. $ran_block = true
  126. end
  127. end
  128. require "#{app_path}/config/environment"
  129. assert !$ran_block
  130. AppTemplate::Application.load_console
  131. assert $ran_block
  132. end
  133. test "railtie can add initializers" do
  134. $ran_block = false
  135. class MyTie < Rails::Railtie
  136. initializer :something_nice do
  137. $ran_block = true
  138. end
  139. end
  140. assert !$ran_block
  141. require "#{app_path}/config/environment"
  142. assert $ran_block
  143. end
  144. test "we can change our environment if we want to" do
  145. begin
  146. original_env = Rails.env
  147. Rails.env = 'foo'
  148. assert_equal('foo', Rails.env)
  149. ensure
  150. Rails.env = original_env
  151. assert_equal(original_env, Rails.env)
  152. end
  153. end
  154. end
  155. end