Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ActionView::TestCase for testing view helpers.
- Loading branch information
Showing
21 changed files
with
190 additions
and
131 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
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'active_support/test_case' | ||
|
||
module ActionView | ||
class NonInferrableHelperError < ActionViewError | ||
def initialize(name) | ||
super "Unable to determine the helper to test from #{name}. " + | ||
"You'll need to specify it using tests YourHelper in your " + | ||
"test case definition" | ||
end | ||
end | ||
|
||
class TestCase < ActiveSupport::TestCase | ||
class_inheritable_accessor :helper_class | ||
@@helper_class = nil | ||
|
||
class << self | ||
def tests(helper_class) | ||
self.helper_class = helper_class | ||
end | ||
|
||
def helper_class | ||
if current_helper_class = read_inheritable_attribute(:helper_class) | ||
current_helper_class | ||
else | ||
self.helper_class = determine_default_helper_class(name) | ||
end | ||
end | ||
|
||
def determine_default_helper_class(name) | ||
name.sub(/Test$/, '').constantize | ||
rescue NameError | ||
raise NonInferrableHelperError.new(name) | ||
end | ||
end | ||
|
||
ActionView::Base.helper_modules.each do |helper_module| | ||
include helper_module | ||
end | ||
include ActionController::PolymorphicRoutes | ||
include ActionController::RecordIdentifier | ||
|
||
setup :setup_with_helper_class | ||
|
||
def setup_with_helper_class | ||
self.class.send(:include, helper_class) | ||
end | ||
|
||
class TestController < ActionController::Base | ||
attr_accessor :request, :response | ||
|
||
def initialize | ||
@request = ActionController::TestRequest.new | ||
@response = ActionController::TestResponse.new | ||
end | ||
end | ||
|
||
private | ||
def method_missing(selector, *args) | ||
controller = TestController.new | ||
return controller.send!(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector) | ||
super | ||
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
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
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
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
Oops, something went wrong.