Skip to content

Commit

Permalink
Introduce TestCase subclasses for testing rails applications allowing…
Browse files Browse the repository at this point in the history
… tests to be DRY'd up a bit and to provide a path toward tidying up our monkeypatching of test/unit.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8022 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
NZKoz committed Oct 26, 2007
1 parent 2bfd677 commit 2cc0cac
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 40 deletions.
2 changes: 2 additions & 0 deletions actionmailer/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *SVN*


* Introduce a new base test class for testing Mailers. ActionMailer::TestCase [Koz]

* Fix silent failure of rxml templates. #9879 [jstewart] * Fix silent failure of rxml templates. #9879 [jstewart]




Expand Down
59 changes: 59 additions & 0 deletions actionmailer/lib/action_mailer/test_case.rb
@@ -0,0 +1,59 @@
require 'active_support/test_case'

module ActionMailer
class NonInferrableMailerError < ::StandardError
def initialize(name)
super "Unable to determine the mailer to test from #{name}. " +
"You'll need to specify it using tests YourMailer in your " +
"test case definition"
end
end
# New Test Super class for forward compatibility.
# To override
class TestCase < ActiveSupport::TestCase
include ActionMailer::Quoting

class << self
def tests(mailer)
write_inheritable_attribute(:mailer_class, mailer)
end

def mailer_class
if mailer = read_inheritable_attribute(:mailer_class)
mailer
else
tests determine_default_mailer(name)
end
end

def determine_default_mailer(name)
name.sub(/Test$/, '').constantize
rescue NameError => e
raise NonInferrableMailerError.new(name)
end
end

def setup
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries = []

@expected = TMail::Mail.new
@expected.set_content_type "text", "plain", { "charset" => charset }
@expected.mime_version = '1.0'
end

private
def charset
"utf-8"
end

def encode(subject)
quoted_printable(subject, charset)
end

def read_fixture(action)
IO.readlines(File.join(RAILS_ROOT, 'test', 'fixtures', mailer_class.name.underscore, action))
end
end
end
1 change: 1 addition & 0 deletions actionmailer/test/abstract_unit.rb
Expand Up @@ -2,6 +2,7 @@


$:.unshift "#{File.dirname(__FILE__)}/../lib" $:.unshift "#{File.dirname(__FILE__)}/../lib"
require 'action_mailer' require 'action_mailer'
require 'action_mailer/test_case'


# Show backtraces for deprecated behavior for quicker cleanup. # Show backtraces for deprecated behavior for quicker cleanup.
ActiveSupport::Deprecation.debug = true ActiveSupport::Deprecation.debug = true
Expand Down
35 changes: 30 additions & 5 deletions actionmailer/test/test_helper_test.rb
Expand Up @@ -8,13 +8,38 @@ def test
end end
end end


class TestHelperTest < Test::Unit::TestCase class TestHelperMailerTest < ActionMailer::TestCase
def setup
ActionMailer::Base.delivery_method = :test def test_setup_sets_right_action_mailer_options
ActionMailer::Base.perform_deliveries = true assert_equal :test, ActionMailer::Base.delivery_method
ActionMailer::Base.deliveries = [] assert ActionMailer::Base.perform_deliveries
assert_equal [], ActionMailer::Base.deliveries
end

def test_setup_creates_the_expected_mailer
assert @expected.is_a?(TMail::Mail)
assert_equal "1.0", @expected.mime_version
assert_equal "text/plain", @expected.content_type
end

def test_mailer_class_is_correctly_inferred
assert_equal TestHelperMailer, self.class.mailer_class
end

def test_determine_default_mailer_raises_correct_error
assert_raises(ActionMailer::NonInferrableMailerError) do
self.class.determine_default_mailer("NotAMailerTest")
end
end end


def test_charset_is_utf_8
assert_equal "utf-8", charset
end

def test_encode
assert_equal "=?utf-8?Q?=0aasdf=0a?=", encode("\nasdf\n")
end

def test_assert_emails def test_assert_emails
assert_nothing_raised do assert_nothing_raised do
assert_emails 1 do assert_emails 1 do
Expand Down
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *SVN*


* Introduce a new test case class for functional tests. ActionController::TestCase. [Koz]

* Fix incorrect path in helper rdoc. Closes #9926 [viktor tron] * Fix incorrect path in helper rdoc. Closes #9926 [viktor tron]


* Partials also set 'object' to the default partial variable. #8823 [Nick Retallack, Jeremy Kemper] * Partials also set 'object' to the default partial variable. #8823 [Nick Retallack, Jeremy Kemper]
Expand Down
53 changes: 53 additions & 0 deletions actionpack/lib/action_controller/test_case.rb
@@ -0,0 +1,53 @@
require 'active_support/test_case'

module ActionController
class NonInferrableControllerError < ActionControllerError
def initialize(name)
super "Unable to determine the controller to test from #{name}. " +
"You'll need to specify it using tests YourController in your " +
"test case definition"
end
end

class TestCase < ActiveSupport::TestCase
@@controller_class = nil
class << self
def tests(controller_class)
self.controller_class = controller_class
end

def controller_class=(new_class)
prepare_controller_class(new_class)
write_inheritable_attribute(:controller_class, new_class)
end

def controller_class
if current_controller_class = read_inheritable_attribute(:controller_class)
current_controller_class
else
self.controller_class= determine_default_controller_class(name)
end
end

def determine_default_controller_class(name)
name.sub(/Test$/, '').constantize
rescue NameError
raise NonInferrableControllerError.new(name)
end

def prepare_controller_class(new_class)
new_class.class_eval do
def rescue_action(e)
raise e
end
end
end
end

def setup
@controller = self.class.controller_class.new
@request = TestRequest.new
@response = TestResponse.new
end
end
end
32 changes: 32 additions & 0 deletions actionpack/test/controller/test_test.rb
@@ -1,5 +1,6 @@
require "#{File.dirname(__FILE__)}/../abstract_unit" require "#{File.dirname(__FILE__)}/../abstract_unit"
require "#{File.dirname(__FILE__)}/fake_controllers" require "#{File.dirname(__FILE__)}/fake_controllers"
require "action_controller/test_case"


class TestTest < Test::Unit::TestCase class TestTest < Test::Unit::TestCase
class TestController < ActionController::Base class TestController < ActionController::Base
Expand Down Expand Up @@ -580,3 +581,34 @@ def test_should_only_clean_assertion_failure_errors
assert !caught.backtrace.empty? assert !caught.backtrace.empty?
end end
end end

class InferringClassNameTest < Test::Unit::TestCase
def test_determine_controller_class
assert_equal ContentController, determine_class("ContentControllerTest")
end

def test_determine_controller_class_with_nonsense_name
assert_raises ActionController::NonInferrableControllerError do
determine_class("HelloGoodBye")
end
end

def test_determine_controller_class_with_sensible_name_where_no_controller_exists
assert_raises ActionController::NonInferrableControllerError do
determine_class("NoControllerWithThisNameTest")
end
end

private
def determine_class(name)
ActionController::TestCase.determine_default_controller_class(name)
end
end

class CrazyNameTest < ActionController::TestCase
tests ContentController
def test_controller_class_can_be_set_manually_not_just_inferred
assert_equal ContentController, self.class.controller_class
end
end

5 changes: 5 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,10 @@
*SVN* *SVN*


* Introduce a base class for all test cases used by rails applications. ActiveSupport::TestCase [Koz]

The intention is to use this to reduce the amount of monkeypatching / overriding that
is done to test/unit's classes.

* Document Enumerable and Hash #to_json. #9970 [Chu Yeow] * Document Enumerable and Hash #to_json. #9970 [Chu Yeow]


* Hash#to_xml handles symbol values. #9954 [Assaf] * Hash#to_xml handles symbol values. #9954 [Assaf]
Expand Down
2 changes: 2 additions & 0 deletions activesupport/lib/active_support.rb
Expand Up @@ -45,3 +45,5 @@


require 'active_support/multibyte' require 'active_support/multibyte'


require 'active_support/testing'

5 changes: 5 additions & 0 deletions activesupport/lib/active_support/test_case.rb
@@ -0,0 +1,5 @@
module ActiveSupport
class TestCase < Test::Unit::TestCase
include ActiveSupport::Testing::Default
end
end
1 change: 1 addition & 0 deletions activesupport/lib/active_support/testing.rb
@@ -0,0 +1 @@
require 'active_support/testing/default'
11 changes: 11 additions & 0 deletions activesupport/lib/active_support/testing/default.rb
@@ -0,0 +1,11 @@
module ActiveSupport
module Testing
module Default
def run(*args)
return if method_name == :default_test
super
end
end
end
end

@@ -1,15 +1,7 @@
require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper' require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper'
require '<%= file_path %>_controller'


# Re-raise errors caught by the controller. class <%= class_name %>ControllerTest < ActionController::TestCase
class <%= class_name %>Controller; def rescue_action(e) raise e end; end tests <%= class_name %>Controller
class <%= class_name %>ControllerTest < Test::Unit::TestCase
def setup
@controller = <%= class_name %>Controller.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end


# Replace this with your real tests. # Replace this with your real tests.
def test_truth def test_truth
Expand Down
@@ -1,21 +1,7 @@
require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper' require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper'


class <%= class_name %>Test < Test::Unit::TestCase class <%= class_name %>Test < ActionMailer::TestCase
FIXTURES_PATH = File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../fixtures' tests <%= class_name %>
CHARSET = "utf-8"
include ActionMailer::Quoting
def setup
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries = []
@expected = TMail::Mail.new
@expected.set_content_type "text", "plain", { "charset" => CHARSET }
@expected.mime_version = '1.0'
end
<% for action in actions -%> <% for action in actions -%>
def test_<%= action %> def test_<%= action %>
@expected.subject = '<%= class_name %>#<%= action %>' @expected.subject = '<%= class_name %>#<%= action %>'
Expand All @@ -26,12 +12,10 @@ def test_<%= action %>
end end


<% end -%> <% end -%>
private <% if actions.blank? -%>
def read_fixture(action) # replace this with your real tests
IO.readlines("#{FIXTURES_PATH}/<%= file_path %>/#{action}") def test_truth
end assert true

end
def encode(subject) <% end -%>
quoted_printable(subject, CHARSET)
end
end end
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper' require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper'


class <%= class_name %>Test < Test::Unit::TestCase class <%= class_name %>Test < ActiveSupport::TestCase
# Replace this with your real tests. # Replace this with your real tests.
def test_truth def test_truth
assert true assert true
Expand Down
4 changes: 4 additions & 0 deletions railties/lib/test_help.rb
Expand Up @@ -5,9 +5,13 @@
silence_warnings { RAILS_ENV = "test" } silence_warnings { RAILS_ENV = "test" }


require 'test/unit' require 'test/unit'
require 'active_support/test_case'
require 'active_record/fixtures' require 'active_record/fixtures'
require 'active_record/test_case'
require 'action_controller/test_case'
require 'action_controller/test_process' require 'action_controller/test_process'
require 'action_controller/integration' require 'action_controller/integration'
require 'action_mailer/test_case'


Test::Unit::TestCase.fixture_path = RAILS_ROOT + "/test/fixtures/" Test::Unit::TestCase.fixture_path = RAILS_ROOT + "/test/fixtures/"
ActionController::IntegrationTest.fixture_path = Test::Unit::TestCase.fixture_path ActionController::IntegrationTest.fixture_path = Test::Unit::TestCase.fixture_path
Expand Down

0 comments on commit 2cc0cac

Please sign in to comment.