test_case_test.rb 6.5 KB
Newer Older
1
require 'abstract_unit'
2
require 'controller/fake_controllers'
3

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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)
27
            assert_equal 'Holla!', from_shared_helper
28 29 30 31 32 33 34 35 36 37 38 39 40
          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

41 42 43 44 45
      test "can render a layout with block" do
        assert_equal "Before (ChrisCruft)\n!\nAfter",
                      render(:layout => "test/layout_for_partial", :locals => {:name => "ChrisCruft"}) {"!"}
      end

46 47 48
      helper AnotherTestHelper
      test "additional helper classes can be specified as in a controller" do
        assert test_case.ancestors.include?(AnotherTestHelper)
49
        assert_equal 'Howdy!', from_another_helper
50
      end
51 52 53 54 55 56 57 58 59 60 61 62 63 64

      test "determine_default_helper_class returns nil if name.sub(/Test$/, '').constantize resolves to a class" do
        assert_nil self.class.determine_default_helper_class("String")
      end

      test "delegates notice to request.flash" do
        _view.request.flash.expects(:notice).with("this message")
        _view.notice("this message")
      end

      test "delegates alert to request.flash" do
        _view.request.flash.expects(:alert).with("this message")
        _view.alert("this message")
      end
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    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)
80
        assert_equal 'Howdy!', from_another_helper
81 82 83 84 85 86

        test_case.helper_class.module_eval do
          def render_from_helper
            from_another_helper
          end
        end
87
        assert_equal 'Howdy!', render(:partial => 'test/from_helper')
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
      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
106 107 108 109 110 111 112
        # This ensure is a hidious hack to deal with these tests bleeding
        # methods between eachother
        begin
          self.class.helper_class.module_eval do
            def render_from_helper
              render :partial => 'customer', :collection => @customers
            end
113 114
          end

115
          TestController.stubs(:controller_path).returns('test')
116

117 118 119 120
          @customers = [stub(:name => 'Eloy'), stub(:name => 'Manfred')]
          assert_match /Hello: EloyHello: Manfred/, render(:partial => 'test/from_helper')

        ensure
C
Carlhuda 已提交
121
          self.class.helper_class.send(:remove_method, :render_from_helper)
122
        end
123 124 125 126 127 128 129 130
      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
J
Joshua Peek 已提交
131
        controller.request.assign_parameters(@routes, 'foo', 'index')
132 133 134 135 136 137
        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|
J
Joshua Peek 已提交
138
          set.draw { |map| resources :contents }
139 140 141 142 143 144 145
          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|
J
Joshua Peek 已提交
146
          set.draw { |map| resources :contents }
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
          _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
171 172 173 174 175 176 177 178
        # This ensure is a hidious hack to deal with these tests bleeding
        # methods between eachother
        begin
          _helpers.module_eval do
            def render_from_helper; from_test_case end
          end
          assert_equal 'Word!', render(:partial => 'test/from_helper')
        ensure
C
Carlhuda 已提交
179
          _helpers.send(:remove_method, :render_from_helper)
180 181 182 183 184 185 186 187 188 189
        end
      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
190
          safe_concat render(:text => '<ul><li>foo</li></ul>')
191 192 193 194 195 196 197 198 199 200 201 202
        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
203 204
  end
end