Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce TestCase subclasses for testing rails applications allowing…
… 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
Showing
16 changed files
with
218 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
module ActiveSupport | |||
class TestCase < Test::Unit::TestCase | |||
include ActiveSupport::Testing::Default | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
require 'active_support/testing/default' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
module ActiveSupport | |||
module Testing | |||
module Default | |||
def run(*args) | |||
return if method_name == :default_test | |||
super | |||
end | |||
end | |||
end | |||
end | |||
|
12 changes: 2 additions & 10 deletions
12
railties/lib/rails_generator/generators/components/controller/templates/functional_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
railties/lib/rails_generator/generators/components/model/templates/unit_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters