2009-09-19 13:10:41 -04:00
|
|
|
require 'abstract_unit'
|
2012-03-10 16:14:30 -05:00
|
|
|
require 'set'
|
2009-02-24 20:25:21 -05:00
|
|
|
|
|
|
|
module AbstractController
|
|
|
|
module Testing
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-03-17 21:04:22 -04:00
|
|
|
# Test basic dispatching.
|
|
|
|
# ====
|
|
|
|
# * Call process
|
|
|
|
# * Test that the response_body is set correctly
|
2009-02-24 20:25:21 -05:00
|
|
|
class SimpleController < AbstractController::Base
|
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-02-24 20:25:21 -05:00
|
|
|
class Me < SimpleController
|
|
|
|
def index
|
|
|
|
self.response_body = "Hello world"
|
|
|
|
"Something else"
|
2010-04-04 22:57:55 -04:00
|
|
|
end
|
2009-02-24 20:25:21 -05:00
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-02-24 20:25:21 -05:00
|
|
|
class TestBasic < ActiveSupport::TestCase
|
|
|
|
test "dispatching works" do
|
2009-08-26 03:18:52 -04:00
|
|
|
controller = Me.new
|
|
|
|
controller.process(:index)
|
|
|
|
assert_equal "Hello world", controller.response_body
|
2009-02-24 20:25:21 -05:00
|
|
|
end
|
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-03-17 21:04:22 -04:00
|
|
|
# Test Render mixin
|
|
|
|
# ====
|
2009-02-24 20:25:21 -05:00
|
|
|
class RenderingController < AbstractController::Base
|
2013-07-05 08:34:39 -04:00
|
|
|
include AbstractController::Rendering
|
2013-06-28 10:46:10 -04:00
|
|
|
include ActionView::Rendering
|
2009-03-19 18:45:48 -04:00
|
|
|
|
2010-09-17 16:39:14 -04:00
|
|
|
def _prefixes
|
|
|
|
[]
|
|
|
|
end
|
2009-03-19 18:45:48 -04:00
|
|
|
|
2009-03-20 19:50:51 -04:00
|
|
|
def render(options = {})
|
|
|
|
if options.is_a?(String)
|
|
|
|
options = {:_template_name => options}
|
|
|
|
end
|
2009-03-19 18:45:48 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2009-02-24 20:25:21 -05:00
|
|
|
append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views"))
|
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-02-24 20:25:21 -05:00
|
|
|
class Me2 < RenderingController
|
|
|
|
def index
|
|
|
|
render "index.erb"
|
|
|
|
end
|
2010-04-04 22:49:12 -04:00
|
|
|
|
|
|
|
def index_to_string
|
2011-09-22 09:37:38 -04:00
|
|
|
self.response_body = render_to_string "index"
|
2010-04-04 22:49:12 -04:00
|
|
|
end
|
|
|
|
|
2009-02-27 14:42:13 -05:00
|
|
|
def action_with_ivars
|
|
|
|
@my_ivar = "Hello"
|
|
|
|
render "action_with_ivars.erb"
|
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-02-27 14:42:13 -05:00
|
|
|
def naked_render
|
|
|
|
render
|
|
|
|
end
|
2009-04-21 19:02:30 -04:00
|
|
|
|
|
|
|
def rendering_to_body
|
2011-09-22 09:37:38 -04:00
|
|
|
self.response_body = render_to_body :template => "naked_render"
|
2009-04-21 19:02:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def rendering_to_string
|
2011-09-22 09:37:38 -04:00
|
|
|
self.response_body = render_to_string :template => "naked_render"
|
2009-04-21 19:02:30 -04:00
|
|
|
end
|
2009-02-24 20:25:21 -05:00
|
|
|
end
|
2009-08-06 17:48:48 -04:00
|
|
|
|
|
|
|
class TestRenderingController < ActiveSupport::TestCase
|
2009-08-26 03:18:52 -04:00
|
|
|
def setup
|
|
|
|
@controller = Me2.new
|
|
|
|
end
|
|
|
|
|
2009-02-24 20:25:21 -05:00
|
|
|
test "rendering templates works" do
|
2009-08-26 03:18:52 -04:00
|
|
|
@controller.process(:index)
|
|
|
|
assert_equal "Hello from index.erb", @controller.response_body
|
2009-02-24 20:25:21 -05:00
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2010-04-04 22:49:12 -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
|
|
|
|
|
2009-02-27 14:42:13 -05:00
|
|
|
test "rendering passes ivars to the view" do
|
2009-08-26 03:18:52 -04:00
|
|
|
@controller.process(:action_with_ivars)
|
|
|
|
assert_equal "Hello from index_with_ivars.erb", @controller.response_body
|
2009-02-27 14:42:13 -05:00
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-02-27 14:42:13 -05:00
|
|
|
test "rendering with no template name" do
|
2009-08-26 03:18:52 -04:00
|
|
|
@controller.process(:naked_render)
|
|
|
|
assert_equal "Hello from naked_render.erb", @controller.response_body
|
2009-02-27 14:42:13 -05:00
|
|
|
end
|
2009-04-21 19:02:30 -04:00
|
|
|
|
|
|
|
test "rendering to a rack body" do
|
2009-08-26 03:18:52 -04:00
|
|
|
@controller.process(:rendering_to_body)
|
|
|
|
assert_equal "Hello from naked_render.erb", @controller.response_body
|
2009-04-21 19:02:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "rendering to a string" do
|
2009-08-26 03:18:52 -04:00
|
|
|
@controller.process(:rendering_to_string)
|
|
|
|
assert_equal "Hello from naked_render.erb", @controller.response_body
|
2009-04-21 19:02:30 -04:00
|
|
|
end
|
2009-02-27 14:42:13 -05:00
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-03-17 21:04:22 -04:00
|
|
|
# Test rendering with prefixes
|
|
|
|
# ====
|
|
|
|
# * self._prefix is used when defined
|
2009-02-27 14:42:13 -05:00
|
|
|
class PrefixedViews < RenderingController
|
|
|
|
private
|
|
|
|
def self.prefix
|
|
|
|
name.underscore
|
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2010-09-17 16:39:14 -04:00
|
|
|
def _prefixes
|
|
|
|
[self.class.prefix]
|
2009-02-27 14:42:13 -05:00
|
|
|
end
|
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-02-27 14:42:13 -05:00
|
|
|
class Me3 < PrefixedViews
|
|
|
|
def index
|
|
|
|
render
|
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-02-27 14:42:13 -05:00
|
|
|
def formatted
|
|
|
|
self.formats = [:html]
|
|
|
|
render
|
|
|
|
end
|
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-02-27 14:42:13 -05:00
|
|
|
class TestPrefixedViews < ActiveSupport::TestCase
|
2009-08-26 03:18:52 -04:00
|
|
|
def setup
|
|
|
|
@controller = Me3.new
|
|
|
|
end
|
|
|
|
|
2009-02-27 14:42:13 -05:00
|
|
|
test "templates are located inside their 'prefix' folder" do
|
2009-08-26 03:18:52 -04:00
|
|
|
@controller.process(:index)
|
|
|
|
assert_equal "Hello from me3/index.erb", @controller.response_body
|
2009-02-27 14:42:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "templates included their format" do
|
2009-08-26 03:18:52 -04:00
|
|
|
@controller.process(:formatted)
|
|
|
|
assert_equal "Hello from me3/formatted.html.erb", @controller.response_body
|
2009-02-27 14:42:13 -05:00
|
|
|
end
|
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2014-05-10 02:52:44 -04:00
|
|
|
class OverridingLocalPrefixes < AbstractController::Base
|
|
|
|
include AbstractController::Rendering
|
|
|
|
include ActionView::Rendering
|
|
|
|
append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views"))
|
|
|
|
|
|
|
|
def index
|
|
|
|
render
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.local_prefixes
|
|
|
|
# this would usually return "abstract_controller/testing/overriding_local_prefixes"
|
|
|
|
super + ["abstract_controller/testing/me3"]
|
|
|
|
end
|
|
|
|
|
|
|
|
class Inheriting < self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-04 13:18:09 -05:00
|
|
|
class OverridingLocalPrefixesTest < ActiveSupport::TestCase
|
2014-05-14 12:47:14 -04:00
|
|
|
test "overriding .local_prefixes adds prefix" do
|
2014-05-10 02:52:44 -04:00
|
|
|
@controller = OverridingLocalPrefixes.new
|
|
|
|
@controller.process(:index)
|
|
|
|
assert_equal "Hello from me3/index.erb", @controller.response_body
|
|
|
|
end
|
|
|
|
|
2014-05-14 12:47:14 -04:00
|
|
|
test ".local_prefixes is inherited" do
|
2014-05-10 02:52:44 -04:00
|
|
|
@controller = OverridingLocalPrefixes::Inheriting.new
|
|
|
|
@controller.process(:index)
|
|
|
|
assert_equal "Hello from me3/index.erb", @controller.response_body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-17 21:04:22 -04:00
|
|
|
# Test rendering with layouts
|
|
|
|
# ====
|
|
|
|
# self._layout is used when defined
|
2009-02-27 14:42:13 -05:00
|
|
|
class WithLayouts < PrefixedViews
|
2013-06-28 10:46:10 -04:00
|
|
|
include ActionView::Layouts
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-02-27 14:42:13 -05:00
|
|
|
private
|
|
|
|
def self.layout(formats)
|
2013-01-05 00:45:18 -05:00
|
|
|
find_template(name.underscore, {:formats => formats}, :_prefixes => ["layouts"])
|
|
|
|
rescue ActionView::MissingTemplate
|
2009-02-27 14:42:13 -05:00
|
|
|
begin
|
2013-01-05 00:45:18 -05:00
|
|
|
find_template("application", {:formats => formats}, :_prefixes => ["layouts"])
|
2009-02-27 14:42:13 -05:00
|
|
|
rescue ActionView::MissingTemplate
|
|
|
|
end
|
|
|
|
end
|
2009-04-27 21:21:26 -04:00
|
|
|
|
2009-04-17 19:34:49 -04:00
|
|
|
def render_to_body(options = {})
|
2009-06-09 19:46:42 -04:00
|
|
|
options[:_layout] = options[:layout] || _default_layout({})
|
2009-03-19 18:45:48 -04:00
|
|
|
super
|
2010-04-04 22:57:55 -04:00
|
|
|
end
|
2009-02-27 14:42:13 -05:00
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-02-27 14:42:13 -05:00
|
|
|
class Me4 < WithLayouts
|
|
|
|
def index
|
|
|
|
render
|
|
|
|
end
|
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-02-27 14:42:13 -05:00
|
|
|
class TestLayouts < ActiveSupport::TestCase
|
|
|
|
test "layouts are included" do
|
2009-08-26 03:18:52 -04:00
|
|
|
controller = Me4.new
|
2011-06-10 20:10:06 -04:00
|
|
|
controller.process(:index)
|
2009-08-26 03:18:52 -04:00
|
|
|
assert_equal "Me4 Enter : Hello from me4/index.erb : Exit", controller.response_body
|
2009-02-27 14:42:13 -05:00
|
|
|
end
|
2009-02-24 20:25:21 -05:00
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-03-17 21:04:22 -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
|
|
|
|
2009-03-17 21:04:22 -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
|
|
|
|
2009-03-17 21:04:22 -04:00
|
|
|
class RespondToActionController < AbstractController::Base;
|
|
|
|
def index() self.response_body = "success" end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-03-17 21:04:22 -04:00
|
|
|
def fail() self.response_body = "fail" end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-03-17 21:04:22 -04:00
|
|
|
private
|
2009-05-15 18:57:12 -04:00
|
|
|
|
|
|
|
def method_for_action(action_name)
|
|
|
|
action_name.to_s != "fail" && action_name
|
2009-03-17 21:04:22 -04:00
|
|
|
end
|
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-03-17 21:04:22 -04:00
|
|
|
class TestRespondToAction < ActiveSupport::TestCase
|
2009-08-26 03:18:52 -04:00
|
|
|
|
2009-03-17 21:04:22 -04:00
|
|
|
def assert_dispatch(klass, body = "success", action = :index)
|
2009-08-26 03:18:52 -04:00
|
|
|
controller = klass.new
|
|
|
|
controller.process(action)
|
|
|
|
assert_equal body, controller.response_body
|
2009-03-17 21:04:22 -04:00
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-03-17 21:04:22 -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
|
|
|
|
2009-03-17 21:04:22 -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) }
|
2009-03-17 21:04:22 -04:00
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2009-03-17 21:04:22 -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
|
|
|
|
2011-06-08 03:05:25 -04:00
|
|
|
test "a method is available as an action if method_for_action returns true" do
|
2009-03-17 21:04:22 -04:00
|
|
|
assert_dispatch RespondToActionController, "success", :index
|
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2011-06-08 03:05:25 -04:00
|
|
|
test "raises ActionNotFound if method is defined but method_for_action returns false" do
|
2009-06-08 19:14:38 -04:00
|
|
|
assert_raise(ActionNotFound) { RespondToActionController.new.process(:fail) }
|
2009-03-17 21:04:22 -04:00
|
|
|
end
|
|
|
|
end
|
2010-04-04 22:57:55 -04:00
|
|
|
|
2010-08-04 12:58:18 -04:00
|
|
|
class Me6 < AbstractController::Base
|
|
|
|
self.action_methods
|
|
|
|
|
|
|
|
def index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestActionMethodsReloading < ActiveSupport::TestCase
|
|
|
|
|
|
|
|
test "action_methods should be reloaded after defining a new method" do
|
2012-03-10 15:36:00 -05:00
|
|
|
assert_equal Set.new(["index"]), Me6.action_methods
|
2010-08-04 12:58:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-24 20:25:21 -05:00
|
|
|
end
|
2009-04-17 19:34:49 -04:00
|
|
|
end
|