Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10056: actionview_testunit.2.diff

File actionview_testunit.2.diff, 3.6 kB (added by josh, 10 months ago)
  • /dev/null

    old new  
     1require 'active_support/test_case' 
     2 
     3module ActionView 
     4  class NonInferrableHelperError < ActionViewError 
     5    def initialize(name) 
     6      super "Unable to determine the helper to test from #{name}. " + 
     7        "You'll need to specify it using tests YourHelper in your " + 
     8        "test case definition" 
     9    end 
     10  end 
     11 
     12  class TestCase < ActiveSupport::TestCase 
     13    class_inheritable_accessor :helper_class 
     14    @@helper_class = nil 
     15 
     16    class << self 
     17      def tests(helper_class) 
     18        self.helper_class = helper_class 
     19      end 
     20 
     21      def helper_class 
     22        if current_helper_class = read_inheritable_attribute(:helper_class) 
     23          current_helper_class 
     24        else 
     25          self.helper_class = determine_default_helper_class(name) 
     26        end 
     27      end 
     28 
     29      def determine_default_helper_class(name) 
     30        name.sub(/Test$/, '').constantize 
     31      rescue NameError 
     32        raise NonInferrableHelperError.new(name) 
     33      end 
     34    end 
     35 
     36    # TODO: Reuse load helpers method 
     37    # ActionView::Base.load_helpers 
     38 
     39    Dir.entries("#{File.dirname(__FILE__)}/helpers").sort.each do |file| 
     40      next unless file =~ /^([a-z][a-z_]*_helper).rb$/ 
     41      require "action_view/helpers/#{$1}" 
     42      helper_module_name = $1.camelize 
     43      if Helpers.const_defined?(helper_module_name) 
     44        include Helpers.const_get(helper_module_name) 
     45      end 
     46    end 
     47 
     48    include ActionController::PolymorphicRoutes 
     49    include ActionController::RecordIdentifier 
     50 
     51    def setup 
     52      self.class.send(:include, helper_class) 
     53    end 
     54 
     55    class TestController < ActionController::Base 
     56      attr_accessor :request 
     57 
     58      def initialize 
     59        @request = ActionController::TestRequest.new 
     60      end 
     61    end 
     62 
     63    private 
     64      def method_missing(selector, *args) 
     65        controller = TestController.new 
     66        return controller.send!(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector) 
     67        super 
     68      end 
     69  end 
     70end 
  • /dev/null

    old new  
     1require "#{File.dirname(__FILE__)}/../abstract_unit" 
     2require "action_view/test_case" 
     3 
     4ActionController::Routing::Routes.draw do |map| 
     5  map.people 'people', :controller => 'people', :action => 'index' 
     6  map.connect ':controller/:action/:id' 
     7end 
     8 
     9module PeopleHelper 
     10  def title(text) 
     11    content_tag(:h1, text) 
     12  end 
     13 
     14  def homepage_path 
     15    people_path 
     16  end 
     17 
     18  def homepage_url 
     19    people_url 
     20  end 
     21 
     22  def link_to_person(person) 
     23    link_to person.name, person 
     24  end 
     25end 
     26 
     27class PeopleHelperTest < ActionView::TestCase 
     28  def test_title 
     29    assert_equal "<h1>Ruby on Rails</h1>", title("Ruby on Rails") 
     30  end 
     31 
     32  def test_homepage_path 
     33    assert_equal "/people", homepage_path 
     34  end 
     35 
     36  def test_homepage_url 
     37    assert_equal "http://test.host/people", homepage_url 
     38  end 
     39 
     40  uses_mocha "link_to_person" do 
     41    def test_link_to_person 
     42      person = mock(:name => "David") 
     43      expects(:mocha_mock_path).with(person).returns("/people/1") 
     44      assert_equal '<a href="/people/1">David</a>', link_to_person(person) 
     45    end 
     46  end 
     47end 
     48 
     49class CrazyHelperTest < ActionView::TestCase 
     50  tests PeopleHelper 
     51 
     52  def test_helper_class_can_be_set_manually_not_just_inferred 
     53    assert_equal PeopleHelper, self.class.helper_class 
     54  end 
     55end