aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/test')
-rw-r--r--actionpack/test/abstract_unit.rb21
-rw-r--r--actionpack/test/activerecord/active_record_store_test.rb32
-rw-r--r--actionpack/test/controller/caching_test.rb5
-rw-r--r--actionpack/test/controller/dispatcher_test.rb32
-rw-r--r--actionpack/test/controller/integration_test.rb11
-rw-r--r--actionpack/test/controller/logging_test.rb23
-rw-r--r--actionpack/test/controller/rescue_test.rb7
-rw-r--r--actionpack/test/controller/webservice_test.rb4
-rw-r--r--actionpack/test/dispatch/session/cookie_store_test.rb28
-rw-r--r--actionpack/test/dispatch/session/mem_cache_store_test.rb19
-rw-r--r--actionpack/test/dispatch/show_exceptions_test.rb8
-rw-r--r--actionpack/test/dispatch/static_test.rb35
-rw-r--r--actionpack/test/fixtures/public/foo/bar.html1
-rw-r--r--actionpack/test/fixtures/public/foo/index.html1
-rw-r--r--actionpack/test/fixtures/public/index.html1
-rw-r--r--actionpack/test/fixtures/test/_from_helper.erb1
-rw-r--r--actionpack/test/template/active_record_helper_test.rb2
-rw-r--r--actionpack/test/template/benchmark_helper_test.rb12
-rw-r--r--actionpack/test/template/date_helper_i18n_test.rb19
-rw-r--r--actionpack/test/template/date_helper_test.rb42
-rw-r--r--actionpack/test/template/form_options_helper_test.rb2
-rw-r--r--actionpack/test/template/url_helper_test.rb14
-rw-r--r--actionpack/test/view/test_case_test.rb171
23 files changed, 326 insertions, 165 deletions
diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb
index b9293ffb9f..aef3dd6165 100644
--- a/actionpack/test/abstract_unit.rb
+++ b/actionpack/test/abstract_unit.rb
@@ -27,8 +27,6 @@ require 'action_view/base'
require 'action_dispatch'
require 'active_model'
require 'fixture_template'
-require 'action_controller/testing/process'
-require 'action_controller/testing/integration'
require 'action_view/test_case'
require 'active_support/dependencies'
@@ -54,6 +52,15 @@ ORIGINAL_LOCALES = I18n.available_locales.map {|locale| locale.to_s }.sort
FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
+class ActionController::IntegrationTest < ActiveSupport::TestCase
+ @@app = ActionDispatch::MiddlewareStack.new { |middleware|
+ middleware.use "ActionDispatch::ShowExceptions"
+ middleware.use "ActionDispatch::Callbacks"
+ middleware.use "ActionDispatch::ParamsParser"
+ middleware.use "Rack::Head"
+ }.build(ActionController::Routing::Routes)
+end
+
module ActionView
class TestCase
setup do
@@ -71,10 +78,6 @@ class Rack::TestCase < ActionController::IntegrationTest
ActionController::Base.session_options[:secret] = ("*" * 30)
end
- def app
- @app ||= ActionController::Dispatcher.new
- end
-
def self.testing(klass = nil)
if klass
@testing = "/#{klass.name.underscore}".sub!(/_controller$/, '')
@@ -120,12 +123,6 @@ class ::ApplicationController < ActionController::Base
end
module ActionController
- Base.session = {
- :key => '_testing_session',
- :secret => '8273f16463985e2b3747dc25e30f2528'
- }
- Base.session_store = nil
-
class << Routing
def possible_controllers
@@possible_controllers ||= []
diff --git a/actionpack/test/activerecord/active_record_store_test.rb b/actionpack/test/activerecord/active_record_store_test.rb
index 19d9c955a5..102b9cffdd 100644
--- a/actionpack/test/activerecord/active_record_store_test.rb
+++ b/actionpack/test/activerecord/active_record_store_test.rb
@@ -33,7 +33,6 @@ class ActiveRecordStoreTest < ActionController::IntegrationTest
def setup
ActiveRecord::SessionStore.session_class.create_table!
- reset_app!
end
def teardown
@@ -120,21 +119,15 @@ class ActiveRecordStoreTest < ActionController::IntegrationTest
reset!
- get '/set_session_value', :_session_id => session_id, :foo => "baz"
- assert_response :success
- assert_equal nil, cookies['_session_id']
-
get '/get_session_value', :_session_id => session_id
assert_response :success
assert_equal 'foo: nil', response.body
- assert_equal nil, cookies['_session_id']
+ assert_not_equal session_id, cookies['_session_id']
end
end
def test_allows_session_fixation
- with_test_route_set do
- reset_with_fixation!
-
+ with_test_route_set(:cookie_only => false) do
get '/set_session_value'
assert_response :success
assert cookies['_session_id']
@@ -145,7 +138,7 @@ class ActiveRecordStoreTest < ActionController::IntegrationTest
session_id = cookies['_session_id']
assert session_id
- reset_with_fixation!
+ reset!
get '/set_session_value', :_session_id => session_id, :foo => "baz"
assert_response :success
@@ -159,24 +152,13 @@ class ActiveRecordStoreTest < ActionController::IntegrationTest
end
private
- def reset_app!
- app = ActiveRecord::SessionStore.new(ActionController::Dispatcher.new, :key => '_session_id')
- @integration_session = open_session(app)
- end
-
- def reset_with_fixation!
- app = ActiveRecord::SessionStore.new(ActionController::Dispatcher.new, :key => '_session_id', :cookie_only => false)
- @integration_session = open_session(app)
- end
-
- def with_test_route_set
+ def with_test_route_set(options = {})
with_routing do |set|
set.draw do |map|
- map.with_options :controller => "active_record_store_test/test" do |c|
- c.connect "/:action"
- end
+ map.connect "/:action", :controller => "active_record_store_test/test"
end
- reset_app!
+ @app = ActiveRecord::SessionStore.new(set, options.reverse_merge(:key => '_session_id'))
+ reset!
yield
end
end
diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb
index bd17df73c7..1a9f95e5e9 100644
--- a/actionpack/test/controller/caching_test.rb
+++ b/actionpack/test/controller/caching_test.rb
@@ -631,9 +631,8 @@ class FragmentCachingTest < ActionController::TestCase
buffer = 'generated till now -> '
@controller.fragment_for(buffer, 'expensive') { fragment_computed = true }
- assert_equal 2, listener.size
- assert_equal :fragment_exist?, listener[0].name
- assert_equal :write_fragment, listener[1].name
+ assert_equal 1, listener.count { |e| e.name == :fragment_exist? }
+ assert_equal 1, listener.count { |e| e.name == :write_fragment }
assert fragment_computed
assert_equal 'generated till now -> ', buffer
diff --git a/actionpack/test/controller/dispatcher_test.rb b/actionpack/test/controller/dispatcher_test.rb
index 150fc83cde..622d67287d 100644
--- a/actionpack/test/controller/dispatcher_test.rb
+++ b/actionpack/test/controller/dispatcher_test.rb
@@ -14,15 +14,12 @@ class DispatcherTest < Test::Unit::TestCase
ActionDispatch::Callbacks.reset_callbacks(:prepare)
ActionDispatch::Callbacks.reset_callbacks(:call)
- @old_router, Dispatcher.router = Dispatcher.router, mock()
- Dispatcher.router.stubs(:call).returns([200, {}, 'response'])
- Dispatcher.router.stubs(:reload)
+ ActionController::Routing::Routes.stubs(:call).returns([200, {}, 'response'])
+ ActionController::Routing::Routes.stubs(:reload)
Dispatcher.stubs(:require_dependency)
end
def teardown
- Dispatcher.router = @old_router
- @dispatcher = nil
ENV.delete 'REQUEST_METHOD'
end
@@ -32,27 +29,22 @@ class DispatcherTest < Test::Unit::TestCase
end
def test_reloads_routes_before_dispatch_if_in_loading_mode
- Dispatcher.router.expects(:reload).once
+ ActionController::Routing::Routes.expects(:reload).once
dispatch(false)
end
def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode
- Dispatcher.router.expects(:reload).never
+ ActionController::Routing::Routes.expects(:reload).never
ActiveSupport::Dependencies.expects(:clear).never
dispatch
end
- # Stub out dispatch error logger
- class << Dispatcher
- def log_failsafe_exception(status, exception); end
- end
-
def test_prepare_callbacks
a = b = c = nil
- Dispatcher.to_prepare { |*args| a = b = c = 1 }
- Dispatcher.to_prepare { |*args| b = c = 2 }
- Dispatcher.to_prepare { |*args| c = 3 }
+ ActionDispatch::Callbacks.to_prepare { |*args| a = b = c = 1 }
+ ActionDispatch::Callbacks.to_prepare { |*args| b = c = 2 }
+ ActionDispatch::Callbacks.to_prepare { |*args| c = 3 }
# Ensure to_prepare callbacks are not run when defined
assert_nil a || b || c
@@ -71,8 +63,8 @@ class DispatcherTest < Test::Unit::TestCase
end
def test_to_prepare_with_identifier_replaces
- Dispatcher.to_prepare(:unique_id) { |*args| Foo.a, Foo.b = 1, 1 }
- Dispatcher.to_prepare(:unique_id) { |*args| Foo.a = 2 }
+ ActionDispatch::Callbacks.to_prepare(:unique_id) { |*args| Foo.a, Foo.b = 1, 1 }
+ ActionDispatch::Callbacks.to_prepare(:unique_id) { |*args| Foo.a = 2 }
dispatch
assert_equal 2, Foo.a
@@ -83,12 +75,8 @@ class DispatcherTest < Test::Unit::TestCase
def dispatch(cache_classes = true)
ActionController::Dispatcher.prepare_each_request = false
Dispatcher.define_dispatcher_callbacks(cache_classes)
- Dispatcher.middleware = ActionDispatch::MiddlewareStack.new do |middleware|
- middlewares = File.expand_path(File.join(File.dirname(__FILE__), "../../lib/action_controller/dispatch/middlewares.rb"))
- middleware.instance_eval(File.read(middlewares))
- end
- @dispatcher ||= Dispatcher.new
+ @dispatcher ||= ActionDispatch::Callbacks.new(ActionController::Routing::Routes)
@dispatcher.call({'rack.input' => StringIO.new(''), 'action_dispatch.show_exceptions' => false})
end
diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb
index 9f56bbfd46..0e4ca21143 100644
--- a/actionpack/test/controller/integration_test.rb
+++ b/actionpack/test/controller/integration_test.rb
@@ -1,4 +1,5 @@
require 'abstract_unit'
+require 'controller/fake_controllers'
require 'action_controller/vendor/html-scanner'
class SessionTest < Test::Unit::TestCase
@@ -363,6 +364,10 @@ class IntegrationProcessTest < ActionController::IntegrationTest
end
end
+ def test_generate_url_with_controller
+ assert_equal 'http://www.example.com/foo', url_for(:controller => "foo")
+ end
+
private
def with_test_route_set
with_routing do |set|
@@ -389,7 +394,7 @@ class MetalIntegrationTest < ActionController::IntegrationTest
end
def setup
- @integration_session = ActionController::Integration::Session.new(Poller)
+ @app = Poller
end
def test_successful_get
@@ -406,4 +411,8 @@ class MetalIntegrationTest < ActionController::IntegrationTest
assert_response :not_found
assert_equal '', response.body
end
+
+ def test_generate_url_without_controller
+ assert_equal 'http://www.example.com/foo', url_for(:controller => "foo")
+ end
end
diff --git a/actionpack/test/controller/logging_test.rb b/actionpack/test/controller/logging_test.rb
index 98ffbc3813..2b5e8d8bde 100644
--- a/actionpack/test/controller/logging_test.rb
+++ b/actionpack/test/controller/logging_test.rb
@@ -12,11 +12,11 @@ class LoggingTest < ActionController::TestCase
class MockLogger
attr_reader :logged
attr_accessor :level
-
+
def initialize
@level = Logger::DEBUG
end
-
+
def method_missing(method, *args, &blk)
@logged ||= []
@logged << args.first
@@ -31,25 +31,24 @@ class LoggingTest < ActionController::TestCase
def test_logging_without_parameters
get :show
- assert_equal 2, logs.size
+ assert_equal 3, logs.size
assert_nil logs.detect {|l| l =~ /Parameters/ }
end
def test_logging_with_parameters
get :show, :id => '10'
- assert_equal 3, logs.size
+ assert_equal 4, logs.size
params = logs.detect {|l| l =~ /Parameters/ }
assert_equal 'Parameters: {"id"=>"10"}', params
end
-
+
private
+ def set_logger
+ @controller.logger = MockLogger.new
+ end
- def set_logger
- @controller.logger = MockLogger.new
- end
-
- def logs
- @logs ||= @controller.logger.logged.compact.map {|l| l.to_s.strip}
- end
+ def logs
+ @logs ||= @controller.logger.logged.compact.map {|l| l.to_s.strip}
+ end
end
diff --git a/actionpack/test/controller/rescue_test.rb b/actionpack/test/controller/rescue_test.rb
index 09eddfe4a7..6ad708bba1 100644
--- a/actionpack/test/controller/rescue_test.rb
+++ b/actionpack/test/controller/rescue_test.rb
@@ -326,19 +326,16 @@ class RescueTest < ActionController::IntegrationTest
end
test 'rescue routing exceptions' do
- app = ActionDispatch::Rescue.new(ActionController::Routing::Routes) do
+ @app = ActionDispatch::Rescue.new(ActionController::Routing::Routes) do
rescue_from ActionController::RoutingError, lambda { |env| [200, {"Content-Type" => "text/html"}, "Gotcha!"] }
end
- @integration_session = open_session(app)
get '/b00m'
assert_equal "Gotcha!", response.body
end
test 'unrescued exception' do
- app = ActionDispatch::Rescue.new(ActionController::Routing::Routes)
- @integration_session = open_session(app)
-
+ @app = ActionDispatch::Rescue.new(ActionController::Routing::Routes)
assert_raise(ActionController::RoutingError) { get '/b00m' }
end
diff --git a/actionpack/test/controller/webservice_test.rb b/actionpack/test/controller/webservice_test.rb
index 916124e221..c04d20fbad 100644
--- a/actionpack/test/controller/webservice_test.rb
+++ b/actionpack/test/controller/webservice_test.rb
@@ -245,8 +245,8 @@ class WebServiceTest < ActionController::IntegrationTest
private
def with_params_parsers(parsers = {})
old_session = @integration_session
- app = ActionDispatch::ParamsParser.new(ActionController::Routing::Routes, parsers)
- @integration_session = open_session(app)
+ @app = ActionDispatch::ParamsParser.new(ActionController::Routing::Routes, parsers)
+ reset!
yield
ensure
@integration_session = old_session
diff --git a/actionpack/test/dispatch/session/cookie_store_test.rb b/actionpack/test/dispatch/session/cookie_store_test.rb
index d695be0be4..6241c79829 100644
--- a/actionpack/test/dispatch/session/cookie_store_test.rb
+++ b/actionpack/test/dispatch/session/cookie_store_test.rb
@@ -5,9 +5,6 @@ class CookieStoreTest < ActionController::IntegrationTest
SessionKey = '_myapp_session'
SessionSecret = 'b3c631c314c0bbca50c1b2843150fe33'
- # Make sure Session middleware doesnt get included in the middleware stack
- ActionController::Base.session_store = nil
-
Verifier = ActiveSupport::MessageVerifier.new(SessionSecret, 'SHA1')
SignedBar = Verifier.generate(:foo => "bar", :session_id => ActiveSupport::SecureRandom.hex(16))
@@ -46,10 +43,6 @@ class CookieStoreTest < ActionController::IntegrationTest
def rescue_action(e) raise end
end
- def setup
- reset_app!
- end
-
def test_raises_argument_error_if_missing_session_key
assert_raise(ArgumentError, nil.inspect) {
ActionDispatch::Session::CookieStore.new(nil,
@@ -193,10 +186,7 @@ class CookieStoreTest < ActionController::IntegrationTest
end
def test_session_store_with_expire_after
- with_test_route_set do
- app = ActionDispatch::Session::CookieStore.new(ActionController::Dispatcher.new, :key => SessionKey, :secret => SessionSecret, :expire_after => 5.hours)
- @integration_session = open_session(app)
-
+ with_test_route_set(:expire_after => 5.hours) do
# First request accesses the session
time = Time.local(2008, 4, 24)
Time.stubs(:now).returns(time)
@@ -226,20 +216,14 @@ class CookieStoreTest < ActionController::IntegrationTest
end
private
- def reset_app!
- app = ActionDispatch::Session::CookieStore.new(ActionController::Dispatcher.new,
- :key => SessionKey, :secret => SessionSecret)
- @integration_session = open_session(app)
- end
-
- def with_test_route_set
+ def with_test_route_set(options = {})
with_routing do |set|
set.draw do |map|
- map.with_options :controller => "cookie_store_test/test" do |c|
- c.connect "/:action"
- end
+ map.connect "/:action", :controller => "cookie_store_test/test"
end
- reset_app!
+ options = {:key => SessionKey, :secret => SessionSecret}.merge(options)
+ @app = ActionDispatch::Session::CookieStore.new(set, options)
+ reset!
yield
end
end
diff --git a/actionpack/test/dispatch/session/mem_cache_store_test.rb b/actionpack/test/dispatch/session/mem_cache_store_test.rb
index 1588918be7..c2d40ae24a 100644
--- a/actionpack/test/dispatch/session/mem_cache_store_test.rb
+++ b/actionpack/test/dispatch/session/mem_cache_store_test.rb
@@ -32,7 +32,9 @@ class MemCacheStoreTest < ActionController::IntegrationTest
end
begin
- App = ActionDispatch::Session::MemCacheStore.new(ActionController::Dispatcher.new, :key => '_session_id')
+ require 'memcache'
+ memcache = MemCache.new('localhost:11211')
+ memcache.set('ping', '')
def test_setting_and_getting_session_value
with_test_route_set do
@@ -99,7 +101,7 @@ class MemCacheStoreTest < ActionController::IntegrationTest
get '/set_session_value', :_session_id => session_id
assert_response :success
- assert_equal nil, cookies['_session_id']
+ assert_not_equal session_id, cookies['_session_id']
end
end
rescue LoadError, RuntimeError
@@ -107,20 +109,13 @@ class MemCacheStoreTest < ActionController::IntegrationTest
end
private
- def reset_app!
- app = ActionDispatch::Session::MemCacheStore.new(
- ActionController::Dispatcher.new, :key => '_session_id')
- @integration_session = open_session(app)
- end
-
def with_test_route_set
with_routing do |set|
set.draw do |map|
- map.with_options :controller => "mem_cache_store_test/test" do |c|
- c.connect "/:action"
- end
+ map.connect "/:action", :controller => "mem_cache_store_test/test"
end
- reset_app!
+ @app = ActionDispatch::Session::MemCacheStore.new(set, :key => '_session_id')
+ reset!
yield
end
end
diff --git a/actionpack/test/dispatch/show_exceptions_test.rb b/actionpack/test/dispatch/show_exceptions_test.rb
index d4800e4edb..9f6a93756c 100644
--- a/actionpack/test/dispatch/show_exceptions_test.rb
+++ b/actionpack/test/dispatch/show_exceptions_test.rb
@@ -35,7 +35,7 @@ class ShowExceptionsTest < ActionController::IntegrationTest
DevelopmentApp = ActionDispatch::ShowExceptions.new(Boomer, true)
test "rescue in public from a remote ip" do
- @integration_session = open_session(ProductionApp)
+ @app = ProductionApp
self.remote_addr = '208.77.188.166'
get "/"
@@ -52,7 +52,7 @@ class ShowExceptionsTest < ActionController::IntegrationTest
end
test "rescue locally from a local request" do
- @integration_session = open_session(ProductionApp)
+ @app = ProductionApp
self.remote_addr = '127.0.0.1'
get "/"
@@ -73,7 +73,7 @@ class ShowExceptionsTest < ActionController::IntegrationTest
old_locale, I18n.locale = I18n.locale, :da
begin
- @integration_session = open_session(ProductionApp)
+ @app = ProductionApp
self.remote_addr = '208.77.188.166'
get "/"
@@ -89,7 +89,7 @@ class ShowExceptionsTest < ActionController::IntegrationTest
end
test "always rescue locally in development mode" do
- @integration_session = open_session(DevelopmentApp)
+ @app = DevelopmentApp
self.remote_addr = '208.77.188.166'
get "/"
diff --git a/actionpack/test/dispatch/static_test.rb b/actionpack/test/dispatch/static_test.rb
new file mode 100644
index 0000000000..e6957bb0ea
--- /dev/null
+++ b/actionpack/test/dispatch/static_test.rb
@@ -0,0 +1,35 @@
+require 'abstract_unit'
+
+class StaticTest < ActiveSupport::TestCase
+ DummyApp = lambda { |env|
+ [200, {"Content-Type" => "text/plain"}, ["Hello, World!"]]
+ }
+ App = ActionDispatch::Static.new(DummyApp, "#{FIXTURE_LOAD_PATH}/public")
+
+ test "serves dynamic content" do
+ assert_equal "Hello, World!", get("/nofile")
+ end
+
+ test "serves static index at root" do
+ assert_equal "/index.html", get("/index.html")
+ assert_equal "/index.html", get("/index")
+ assert_equal "/index.html", get("/")
+ end
+
+ test "serves static file in directory" do
+ assert_equal "/foo/bar.html", get("/foo/bar.html")
+ assert_equal "/foo/bar.html", get("/foo/bar/")
+ assert_equal "/foo/bar.html", get("/foo/bar")
+ end
+
+ test "serves static index file in directory" do
+ assert_equal "/foo/index.html", get("/foo/index.html")
+ assert_equal "/foo/index.html", get("/foo/")
+ assert_equal "/foo/index.html", get("/foo")
+ end
+
+ private
+ def get(path)
+ Rack::MockRequest.new(App).request("GET", path).body
+ end
+end
diff --git a/actionpack/test/fixtures/public/foo/bar.html b/actionpack/test/fixtures/public/foo/bar.html
new file mode 100644
index 0000000000..9a35646205
--- /dev/null
+++ b/actionpack/test/fixtures/public/foo/bar.html
@@ -0,0 +1 @@
+/foo/bar.html \ No newline at end of file
diff --git a/actionpack/test/fixtures/public/foo/index.html b/actionpack/test/fixtures/public/foo/index.html
new file mode 100644
index 0000000000..497a2e898f
--- /dev/null
+++ b/actionpack/test/fixtures/public/foo/index.html
@@ -0,0 +1 @@
+/foo/index.html \ No newline at end of file
diff --git a/actionpack/test/fixtures/public/index.html b/actionpack/test/fixtures/public/index.html
new file mode 100644
index 0000000000..525950ba6b
--- /dev/null
+++ b/actionpack/test/fixtures/public/index.html
@@ -0,0 +1 @@
+/index.html \ No newline at end of file
diff --git a/actionpack/test/fixtures/test/_from_helper.erb b/actionpack/test/fixtures/test/_from_helper.erb
new file mode 100644
index 0000000000..16de7c0f8a
--- /dev/null
+++ b/actionpack/test/fixtures/test/_from_helper.erb
@@ -0,0 +1 @@
+<%= render_from_helper %> \ No newline at end of file
diff --git a/actionpack/test/template/active_record_helper_test.rb b/actionpack/test/template/active_record_helper_test.rb
index ec3384f15d..c149070f2a 100644
--- a/actionpack/test/template/active_record_helper_test.rb
+++ b/actionpack/test/template/active_record_helper_test.rb
@@ -185,7 +185,7 @@ class ActiveRecordHelperTest < ActionView::TestCase
end
def test_form_with_action_option
- @response.body = form("post", :action => "sign")
+ output_buffer << form("post", :action => "sign")
assert_select "form[action=sign]" do |form|
assert_select "input[type=submit][value=Sign]"
end
diff --git a/actionpack/test/template/benchmark_helper_test.rb b/actionpack/test/template/benchmark_helper_test.rb
index 5d2af7cdd9..ac31fc6503 100644
--- a/actionpack/test/template/benchmark_helper_test.rb
+++ b/actionpack/test/template/benchmark_helper_test.rb
@@ -4,14 +4,14 @@ require 'action_view/helpers/benchmark_helper'
class BenchmarkHelperTest < ActionView::TestCase
tests ActionView::Helpers::BenchmarkHelper
- def teardown
- controller.logger.send(:clear_buffer)
+ def setup
+ super
+ controller.logger = ActiveSupport::BufferedLogger.new(StringIO.new)
+ controller.logger.auto_flushing = false
end
- def controller
- logger = ActiveSupport::BufferedLogger.new(StringIO.new)
- logger.auto_flushing = false
- @controller ||= Struct.new(:logger).new(logger)
+ def teardown
+ controller.logger.send(:clear_buffer)
end
def test_without_block
diff --git a/actionpack/test/template/date_helper_i18n_test.rb b/actionpack/test/template/date_helper_i18n_test.rb
index bc011f59b8..b69a449617 100644
--- a/actionpack/test/template/date_helper_i18n_test.rb
+++ b/actionpack/test/template/date_helper_i18n_test.rb
@@ -20,15 +20,16 @@ class DateHelperDistanceOfTimeInWordsI18nTests < Test::Unit::TestCase
[60.seconds, true] => [:'x_minutes', 1],
# without include_seconds
- [29.seconds, false] => [:'less_than_x_minutes', 1],
- [60.seconds, false] => [:'x_minutes', 1],
- [44.minutes, false] => [:'x_minutes', 44],
- [61.minutes, false] => [:'about_x_hours', 1],
- [24.hours, false] => [:'x_days', 1],
- [30.days, false] => [:'about_x_months', 1],
- [60.days, false] => [:'x_months', 2],
- [1.year, false] => [:'about_x_years', 1],
- [3.years, false] => [:'over_x_years', 3]
+ [29.seconds, false] => [:'less_than_x_minutes', 1],
+ [60.seconds, false] => [:'x_minutes', 1],
+ [44.minutes, false] => [:'x_minutes', 44],
+ [61.minutes, false] => [:'about_x_hours', 1],
+ [24.hours, false] => [:'x_days', 1],
+ [30.days, false] => [:'about_x_months', 1],
+ [60.days, false] => [:'x_months', 2],
+ [1.year, false] => [:'about_x_years', 1],
+ [3.years + 6.months, false] => [:'over_x_years', 3],
+ [3.years + 10.months, false] => [:'almost_x_years', 4]
}.each do |passed, expected|
assert_distance_of_time_in_words_translates_key passed, expected
diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb
index 2e4763f446..9fb2080f77 100644
--- a/actionpack/test/template/date_helper_test.rb
+++ b/actionpack/test/template/date_helper_test.rb
@@ -53,13 +53,14 @@ class DateHelperTest < ActionView::TestCase
assert_equal "about 2 hours", distance_of_time_in_words(from, to + 89.minutes + 30.seconds)
assert_equal "about 24 hours", distance_of_time_in_words(from, to + 23.hours + 59.minutes + 29.seconds)
- # 1440..2879
+ # 1440..2529
assert_equal "1 day", distance_of_time_in_words(from, to + 23.hours + 59.minutes + 30.seconds)
- assert_equal "1 day", distance_of_time_in_words(from, to + 47.hours + 59.minutes + 29.seconds)
+ assert_equal "1 day", distance_of_time_in_words(from, to + 41.hours + 59.minutes + 29.seconds)
- # 2880..43199
- assert_equal "2 days", distance_of_time_in_words(from, to + 47.hours + 59.minutes + 30.seconds)
- assert_equal "29 days", distance_of_time_in_words(from, to + 29.days + 23.hours + 59.minutes + 29.seconds)
+ # 2530..43199
+ assert_equal "2 days", distance_of_time_in_words(from, to + 42.hours + 59.minutes + 30.seconds)
+ assert_equal "3 days", distance_of_time_in_words(from, to + 2.days + 12.hours)
+ assert_equal "30 days", distance_of_time_in_words(from, to + 29.days + 23.hours + 59.minutes + 29.seconds)
# 43200..86399
assert_equal "about 1 month", distance_of_time_in_words(from, to + 29.days + 23.hours + 59.minutes + 30.seconds)
@@ -69,13 +70,28 @@ class DateHelperTest < ActionView::TestCase
assert_equal "2 months", distance_of_time_in_words(from, to + 59.days + 23.hours + 59.minutes + 30.seconds)
assert_equal "12 months", distance_of_time_in_words(from, to + 1.years - 31.seconds)
- # 525600..1051199
- assert_equal "about 1 year", distance_of_time_in_words(from, to + 1.years - 30.seconds)
- assert_equal "about 1 year", distance_of_time_in_words(from, to + 2.years - 31.seconds)
-
- # > 1051199
- assert_equal "over 2 years", distance_of_time_in_words(from, to + 2.years + 30.seconds)
- assert_equal "over 10 years", distance_of_time_in_words(from, to + 10.years)
+ # > 525599
+ assert_equal "about 1 year", distance_of_time_in_words(from, to + 1.years - 30.seconds)
+ assert_equal "about 1 year", distance_of_time_in_words(from, to + 1.years + 3.months - 1.day)
+ assert_equal "over 1 year", distance_of_time_in_words(from, to + 1.years + 6.months)
+
+ assert_equal "almost 2 years", distance_of_time_in_words(from, to + 2.years - 3.months + 1.day)
+ assert_equal "about 2 years", distance_of_time_in_words(from, to + 2.years + 3.months - 1.day)
+ assert_equal "over 2 years", distance_of_time_in_words(from, to + 2.years + 3.months + 1.day)
+ assert_equal "over 2 years", distance_of_time_in_words(from, to + 2.years + 9.months - 1.day)
+ assert_equal "almost 3 years", distance_of_time_in_words(from, to + 2.years + 9.months + 1.day)
+
+ assert_equal "almost 5 years", distance_of_time_in_words(from, to + 5.years - 3.months + 1.day)
+ assert_equal "about 5 years", distance_of_time_in_words(from, to + 5.years + 3.months - 1.day)
+ assert_equal "over 5 years", distance_of_time_in_words(from, to + 5.years + 3.months + 1.day)
+ assert_equal "over 5 years", distance_of_time_in_words(from, to + 5.years + 9.months - 1.day)
+ assert_equal "almost 6 years", distance_of_time_in_words(from, to + 5.years + 9.months + 1.day)
+
+ assert_equal "almost 10 years", distance_of_time_in_words(from, to + 10.years - 3.months + 1.day)
+ assert_equal "about 10 years", distance_of_time_in_words(from, to + 10.years + 3.months - 1.day)
+ assert_equal "over 10 years", distance_of_time_in_words(from, to + 10.years + 3.months + 1.day)
+ assert_equal "over 10 years", distance_of_time_in_words(from, to + 10.years + 9.months - 1.day)
+ assert_equal "almost 11 years", distance_of_time_in_words(from, to + 10.years + 9.months + 1.day)
# test to < from
assert_equal "about 4 hours", distance_of_time_in_words(from + 4.hours, to)
@@ -104,7 +120,7 @@ class DateHelperTest < ActionView::TestCase
def test_distance_in_words_with_dates
start_date = Date.new 1975, 1, 31
end_date = Date.new 1977, 1, 31
- assert_equal("over 2 years", distance_of_time_in_words(start_date, end_date))
+ assert_equal("about 2 years", distance_of_time_in_words(start_date, end_date))
end
def test_distance_in_words_with_integers
diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb
index f3cdab05bf..aa40e46aa8 100644
--- a/actionpack/test/template/form_options_helper_test.rb
+++ b/actionpack/test/template/form_options_helper_test.rb
@@ -1,5 +1,5 @@
require 'abstract_unit'
-require 'active_support/vendor/tzinfo'
+require 'tzinfo'
TZInfo::Timezone.cattr_reader :loaded_zones
diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb
index 447d520ef1..ce99482078 100644
--- a/actionpack/test/template/url_helper_test.rb
+++ b/actionpack/test/template/url_helper_test.rb
@@ -5,8 +5,6 @@ require 'controller/fake_controllers'
RequestMock = Struct.new("Request", :request_uri, :protocol, :host_with_port, :env)
class UrlHelperTest < ActionView::TestCase
- tests ActionView::Helpers::UrlHelper
-
def setup
super
@controller = Class.new do
@@ -386,9 +384,7 @@ class UrlHelperController < ActionController::Base
def rescue_action(e) raise e end
end
-class UrlHelperWithControllerTest < ActionView::TestCase
- tests ActionView::Helpers::UrlHelper
-
+class UrlHelperWithControllerTest < ActionController::TestCase
def setup
super
@request = ActionController::TestRequest.new
@@ -463,9 +459,7 @@ class TasksController < ActionController::Base
end
end
-class LinkToUnlessCurrentWithControllerTest < ActionView::TestCase
- tests ActionView::Helpers::UrlHelper
-
+class LinkToUnlessCurrentWithControllerTest < ActionController::TestCase
def setup
super
@request = ActionController::TestRequest.new
@@ -566,9 +560,7 @@ class SessionsController < ActionController::Base
def rescue_action(e) raise e end
end
-class PolymorphicControllerTest < ActionView::TestCase
- tests ActionView::Helpers::UrlHelper
-
+class PolymorphicControllerTest < ActionController::TestCase
def setup
super
@request = ActionController::TestRequest.new
diff --git a/actionpack/test/view/test_case_test.rb b/actionpack/test/view/test_case_test.rb
index 9124198b28..3e974b87f7 100644
--- a/actionpack/test/view/test_case_test.rb
+++ b/actionpack/test/view/test_case_test.rb
@@ -1,8 +1,171 @@
require 'abstract_unit'
-class TestCaseTest < ActionView::TestCase
- def test_should_have_current_url
- controller = TestController.new
- assert_nothing_raised(NoMethodError){ controller.url_for({:controller => "foo", :action => "index"}) }
+module ActionView
+ class TestCase
+ module ATestHelper
+ end
+
+ module AnotherTestHelper
+ def from_another_helper
+ 'Howdy!'
+ end
+ end
+
+ module ASharedTestHelper
+ def from_shared_helper
+ 'Holla!'
+ end
+ end
+ helper ASharedTestHelper
+
+ module SharedTests
+ def self.included(test_case)
+ test_case.class_eval do
+ test "helpers defined on ActionView::TestCase are available" do
+ assert test_case.ancestors.include?(ASharedTestHelper)
+ assert 'Holla!', from_shared_helper
+ end
+ end
+ end
+ end
+
+ class GeneralViewTest < ActionView::TestCase
+ include SharedTests
+ test_case = self
+
+ test "works without testing a helper module" do
+ assert_equal 'Eloy', render('developers/developer', :developer => stub(:name => 'Eloy'))
+ end
+
+ helper AnotherTestHelper
+ test "additional helper classes can be specified as in a controller" do
+ assert test_case.ancestors.include?(AnotherTestHelper)
+ assert 'Howdy!', from_another_helper
+ end
+ end
+
+ class ClassMethodsTest < ActionView::TestCase
+ include SharedTests
+ test_case = self
+
+ tests ATestHelper
+ test "tests the specified helper module" do
+ assert_equal ATestHelper, test_case.helper_class
+ assert test_case.ancestors.include?(ATestHelper)
+ end
+
+ helper AnotherTestHelper
+ test "additional helper classes can be specified as in a controller" do
+ assert test_case.ancestors.include?(AnotherTestHelper)
+ assert 'Howdy!', from_another_helper
+
+ test_case.helper_class.module_eval do
+ def render_from_helper
+ from_another_helper
+ end
+ end
+ assert 'Howdy!', render(:partial => 'test/from_helper')
+ end
+ end
+
+ class ATestHelperTest < ActionView::TestCase
+ include SharedTests
+ test_case = self
+
+ test "inflects the name of the helper module to test from the test case class" do
+ assert_equal ATestHelper, test_case.helper_class
+ assert test_case.ancestors.include?(ATestHelper)
+ end
+
+ test "a configured test controller is available" do
+ assert_kind_of ActionController::Base, controller
+ assert_equal '', controller.controller_path
+ end
+
+ test "helper class that is being tested is always included in view instance" do
+ self.class.helper_class.module_eval do
+ def render_from_helper
+ render :partial => 'customer', :collection => @customers
+ end
+ end
+
+ TestController.stubs(:controller_path).returns('test')
+
+ @customers = [stub(:name => 'Eloy'), stub(:name => 'Manfred')]
+ assert_match /Hello: EloyHello: Manfred/, render(:partial => 'test/from_helper')
+ end
+
+ test "no additional helpers should shared across test cases" do
+ assert !test_case.ancestors.include?(AnotherTestHelper)
+ assert_raise(NoMethodError) { send :from_another_helper }
+ end
+
+ test "is able to use routes" do
+ controller.request.assign_parameters('foo', 'index')
+ assert_equal '/foo', url_for
+ assert_equal '/bar', url_for(:controller => 'bar')
+ end
+
+ test "is able to use named routes" do
+ with_routing do |set|
+ set.draw { |map| map.resources :contents }
+ assert_equal 'http://test.host/contents/new', new_content_url
+ assert_equal 'http://test.host/contents/1', content_url(:id => 1)
+ end
+ end
+
+ test "named routes can be used from helper included in view" do
+ with_routing do |set|
+ set.draw { |map| map.resources :contents }
+ _helpers.module_eval do
+ def render_from_helper
+ new_content_url
+ end
+ end
+
+ assert_equal 'http://test.host/contents/new', render(:partial => 'test/from_helper')
+ end
+ end
+
+ test "is able to render partials with local variables" do
+ assert_equal 'Eloy', render('developers/developer', :developer => stub(:name => 'Eloy'))
+ assert_equal 'Eloy', render(:partial => 'developers/developer',
+ :locals => { :developer => stub(:name => 'Eloy') })
+ end
+
+ test "is able to render partials from templates and also use instance variables" do
+ TestController.stubs(:controller_path).returns('test')
+
+ @customers = [stub(:name => 'Eloy'), stub(:name => 'Manfred')]
+ assert_match /Hello: EloyHello: Manfred/, render(:file => 'test/list')
+ end
+
+ test "is able to make methods available to the view" do
+ _helpers.module_eval do
+ def render_from_helper; from_test_case end
+ end
+ assert_equal 'Word!', render(:partial => 'test/from_helper')
+ end
+
+ def from_test_case; 'Word!'; end
+ helper_method :from_test_case
+ end
+
+ class AssertionsTest < ActionView::TestCase
+ def render_from_helper
+ form_tag('/foo') do
+ concat render(:text => '<ul><li>foo</li></ul>')
+ end
+ end
+ helper_method :render_from_helper
+
+ test "uses the output_buffer for assert_select" do
+ render(:partial => 'test/from_helper')
+
+ assert_select 'form' do
+ assert_select 'li', :text => 'foo'
+ end
+ end
+ end
end
end