abstract_unit.rb 3.4 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1
$:.unshift(File.dirname(__FILE__) + '/../lib')
2
$:.unshift(File.dirname(__FILE__) + '/../../activesupport/lib')
3
$:.unshift(File.dirname(__FILE__) + '/../../activemodel/lib')
4 5
$:.unshift(File.dirname(__FILE__) + '/lib')

6
$:.unshift(File.dirname(__FILE__) + '/fixtures/helpers')
7
$:.unshift(File.dirname(__FILE__) + '/fixtures/alternate_helpers')
D
Initial  
David Heinemeier Hansson 已提交
8

9 10 11
ENV['new_base'] = "true"
$stderr.puts "Running old tests on new_base"

D
Initial  
David Heinemeier Hansson 已提交
12
require 'test/unit'
13 14 15 16 17
require 'active_support'

require 'active_support/test_case'
require 'action_controller'
require 'fixture_template'
18
require 'action_controller/testing/process'
19 20 21
require 'action_view/test_case'
require 'action_controller/testing/integration'
require 'active_support/dependencies'
Y
Yehuda Katz 已提交
22
require 'active_model'
23

24
$tags[:new_base] = true
D
Initial  
David Heinemeier Hansson 已提交
25

26 27
begin
  require 'ruby-debug'
J
Jeremy Kemper 已提交
28
  Debugger.settings[:autoeval] = true
29
  Debugger.start
30 31 32
rescue LoadError
  # Debugging disabled. `gem install ruby-debug` to enable.
end
33

34
ActiveSupport::Dependencies.hook!
35

36 37 38
# Show backtraces for deprecated behavior for quicker cleanup.
ActiveSupport::Deprecation.debug = true

J
Joshua Peek 已提交
39 40
# Register danish language for testing
I18n.backend.store_translations 'da', {}
41
I18n.backend.store_translations 'pt-BR', {}
42
ORIGINAL_LOCALES = I18n.available_locales.map {|locale| locale.to_s }.sort
J
Joshua Peek 已提交
43

Y
Yehuda Katz + Carl Lerche 已提交
44
FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
45 46 47 48 49 50 51 52 53 54 55 56 57

module ActionView
  class TestCase
    setup do
      ActionController::Routing::Routes.draw do |map|
        map.connect ':controller/:action/:id'
      end
    end
  end
end

module ActionController
  Base.session = {
58 59 60 61
    :key         => '_testing_session',
    :secret      => '8273f16463985e2b3747dc25e30f2528'
  }
  Base.session_store = nil
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

  class Base
    include ActionController::Testing
  end
  
  Base.view_paths = FIXTURE_LOAD_PATH
  
  class TestCase
    include TestProcess
    setup do
      ActionController::Routing::Routes.draw do |map|
        map.connect ':controller/:action/:id'
      end
    end
    
    def assert_template(options = {}, message = nil)
      validate_request!

80
      hax = @controller.view_context.instance_variable_get(:@_rendered)
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

      case options
      when NilClass, String
        rendered = (hax[:template] || []).map { |t| t.identifier }
        msg = build_message(message,
                "expecting <?> but rendering with <?>",
                options, rendered.join(', '))
        assert_block(msg) do
          if options.nil?
            hax[:template].blank?
          else
            rendered.any? { |t| t.match(options) }
          end
        end
      when Hash
        if expected_partial = options[:partial]
          partials = hax[:partials]
          if expected_count = options[:count]
            found = partials.detect { |p, _| p.identifier.match(expected_partial) }
100
            actual_count = found.nil? ? 0 : found[1]
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
            msg = build_message(message,
                    "expecting ? to be rendered ? time(s) but rendered ? time(s)",
                     expected_partial, expected_count, actual_count)
            assert(actual_count == expected_count.to_i, msg)
          else
            msg = build_message(message,
                    "expecting partial <?> but action rendered <?>",
                    options[:partial], partials.keys)
            assert(partials.keys.any? { |p| p.identifier.match(expected_partial) }, msg)
          end
        else
          assert hax[:partials].empty?,
            "Expected no partials to be rendered"
        end
      end
    end
  end
end