1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/test/abstract/abstract_controller_test.rb

255 lines
6.7 KiB
Ruby
Raw Normal View History

2009-09-19 13:10:41 -04:00
require 'abstract_unit'
module AbstractController
module Testing
2010-04-04 22:57:55 -04:00
# Test basic dispatching.
# ====
# * Call process
# * Test that the response_body is set correctly
class SimpleController < AbstractController::Base
end
2010-04-04 22:57:55 -04:00
class Me < SimpleController
def index
self.response_body = "Hello world"
"Something else"
2010-04-04 22:57:55 -04:00
end
end
2010-04-04 22:57:55 -04:00
class TestBasic < ActiveSupport::TestCase
test "dispatching works" do
controller = Me.new
controller.process(:index)
assert_equal "Hello world", controller.response_body
end
end
2010-04-04 22:57:55 -04:00
# Test Render mixin
# ====
class RenderingController < AbstractController::Base
include ::AbstractController::Rendering
def _prefix() end
def render(options = {})
if options.is_a?(String)
options = {:_template_name => options}
end
2010-04-04 22:57:55 -04:00
options[:_prefix] = _prefix
super
end
append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views"))
end
2010-04-04 22:57:55 -04:00
class Me2 < RenderingController
def index
render "index.erb"
end
def index_to_string
self.response_body = render_to_string "index.erb"
end
def action_with_ivars
@my_ivar = "Hello"
render "action_with_ivars.erb"
end
2010-04-04 22:57:55 -04:00
def naked_render
render
end
def rendering_to_body
self.response_body = render_to_body :template => "naked_render.erb"
end
def rendering_to_string
self.response_body = render_to_string :template => "naked_render.erb"
end
end
class TestRenderingController < ActiveSupport::TestCase
def setup
@controller = Me2.new
end
test "rendering templates works" do
@controller.process(:index)
assert_equal "Hello from index.erb", @controller.response_body
end
2010-04-04 22:57:55 -04:00
test "render_to_string works with a String as an argument" do
@controller.process(:index_to_string)
assert_equal "Hello from index.erb", @controller.response_body
end
test "rendering passes ivars to the view" do
@controller.process(:action_with_ivars)
assert_equal "Hello from index_with_ivars.erb", @controller.response_body
end
2010-04-04 22:57:55 -04:00
test "rendering with no template name" do
@controller.process(:naked_render)
assert_equal "Hello from naked_render.erb", @controller.response_body
end
test "rendering to a rack body" do
@controller.process(:rendering_to_body)
assert_equal "Hello from naked_render.erb", @controller.response_body
end
test "rendering to a string" do
@controller.process(:rendering_to_string)
assert_equal "Hello from naked_render.erb", @controller.response_body
end
end
2010-04-04 22:57:55 -04:00
# Test rendering with prefixes
# ====
# * self._prefix is used when defined
class PrefixedViews < RenderingController
private
def self.prefix
name.underscore
end
2010-04-04 22:57:55 -04:00
def _prefix
self.class.prefix
end
end
2010-04-04 22:57:55 -04:00
class Me3 < PrefixedViews
def index
render
end
2010-04-04 22:57:55 -04:00
def formatted
self.formats = [:html]
render
end
end
2010-04-04 22:57:55 -04:00
class TestPrefixedViews < ActiveSupport::TestCase
def setup
@controller = Me3.new
end
test "templates are located inside their 'prefix' folder" do
@controller.process(:index)
assert_equal "Hello from me3/index.erb", @controller.response_body
end
test "templates included their format" do
@controller.process(:formatted)
assert_equal "Hello from me3/formatted.html.erb", @controller.response_body
end
end
2010-04-04 22:57:55 -04:00
# Test rendering with layouts
# ====
# self._layout is used when defined
class WithLayouts < PrefixedViews
include Layouts
2010-04-04 22:57:55 -04:00
private
def self.layout(formats)
begin
find_template(name.underscore, {:formats => formats}, :_prefix => "layouts")
rescue ActionView::MissingTemplate
begin
find_template("application", {:formats => formats}, :_prefix => "layouts")
rescue ActionView::MissingTemplate
end
end
end
2009-04-27 21:21:26 -04:00
def render_to_body(options = {})
options[:_layout] = options[:layout] || _default_layout({})
super
2010-04-04 22:57:55 -04:00
end
end
2010-04-04 22:57:55 -04:00
class Me4 < WithLayouts
def index
render
end
end
2010-04-04 22:57:55 -04:00
class Me5 < WithLayouts
def index
render
end
end
2010-04-04 22:57:55 -04:00
class TestLayouts < ActiveSupport::TestCase
test "layouts are included" do
controller = Me4.new
result = controller.process(:index)
assert_equal "Me4 Enter : Hello from me4/index.erb : Exit", controller.response_body
end
end
2010-04-04 22:57:55 -04:00
# respond_to_action?(action_name)
# ====
# * A method can be used as an action only if this method
# returns true when passed the method name as an argument
# * Defaults to true in AbstractController
class DefaultRespondToActionController < AbstractController::Base
def index() self.response_body = "success" end
end
2010-04-04 22:57:55 -04:00
class ActionMissingRespondToActionController < AbstractController::Base
# No actions
private
def action_missing(action_name)
self.response_body = "success"
end
end
2010-04-04 22:57:55 -04:00
class RespondToActionController < AbstractController::Base;
def index() self.response_body = "success" end
2010-04-04 22:57:55 -04:00
def fail() self.response_body = "fail" end
2010-04-04 22:57:55 -04:00
private
def method_for_action(action_name)
action_name.to_s != "fail" && action_name
end
end
2010-04-04 22:57:55 -04:00
class TestRespondToAction < ActiveSupport::TestCase
def assert_dispatch(klass, body = "success", action = :index)
controller = klass.new
controller.process(action)
assert_equal body, controller.response_body
end
2010-04-04 22:57:55 -04:00
test "an arbitrary method is available as an action by default" do
assert_dispatch DefaultRespondToActionController, "success", :index
end
2010-04-04 22:57:55 -04:00
test "raises ActionNotFound when method does not exist and action_missing is not defined" do
2009-06-08 19:14:38 -04:00
assert_raise(ActionNotFound) { DefaultRespondToActionController.new.process(:fail) }
end
2010-04-04 22:57:55 -04:00
test "dispatches to action_missing when method does not exist and action_missing is defined" do
assert_dispatch ActionMissingRespondToActionController, "success", :ohai
end
2010-04-04 22:57:55 -04:00
test "a method is available as an action if respond_to_action? returns true" do
assert_dispatch RespondToActionController, "success", :index
end
2010-04-04 22:57:55 -04:00
test "raises ActionNotFound if method is defined but respond_to_action? returns false" do
2009-06-08 19:14:38 -04:00
assert_raise(ActionNotFound) { RespondToActionController.new.process(:fail) }
end
end
2010-04-04 22:57:55 -04:00
end
end