Skip to content

Commit

Permalink
refactor ActionView::TestCase internals to track rendered locals
Browse files Browse the repository at this point in the history
this refactoring extracts the semi complex data structure of rendered locals
per view into into a separate class
  • Loading branch information
senny authored and Yves Senn committed Oct 11, 2012
1 parent ed95674 commit d6524d7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
13 changes: 4 additions & 9 deletions actionpack/lib/action_controller/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,12 @@ def assert_template(options = {}, message = nil)

if expected_partial = options[:partial]
if expected_locals = options[:locals]
if defined?(@_locals)
actual_locals_collection = @_locals[expected_partial.to_s.sub(/^_/,'')]
result = actual_locals_collection.any? do |actual_locals|
expected_locals.each_pair.all? do |k,v|
v == actual_locals[k]
end
end
if defined?(@_rendered_views)
view = expected_partial.to_s.sub(/^_/,'')
msg = 'expecting %s to be rendered with %s but was with %s' % [expected_partial,
expected_locals,
actual_locals_collection]
assert(result, msg)
@_rendered_views.locals_for(view)]
assert(@_rendered_views.view_rendered?(view, options[:locals]), msg)
else
warn "the :locals option to #assert_template is only supported in a ActionView::TestCase"
end
Expand Down
40 changes: 29 additions & 11 deletions actionpack/lib/action_view/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,29 @@ def render(options = {}, local_assigns = {}, &block)
output
end

def locals
@_locals ||= {}
def rendered_views
@_rendered_views ||= RenderedViewsCollection.new
end

class RenderedViewsCollection
def initialize
@rendered_views ||= {}
end

def add(view, locals)
@rendered_views[view] ||= []
@rendered_views[view] << locals
end

def locals_for(view)
@rendered_views[view]
end

def view_rendered?(view, expected_locals)
locals_for(view).any? do |actual_locals|
expected_locals.all? {|key, value| value == actual_locals[key] }
end
end
end

included do
Expand Down Expand Up @@ -156,21 +177,18 @@ def make_test_case_available_to_view!
end

module Locals
attr_accessor :locals
attr_accessor :rendered_views

def render(options = {}, local_assigns = {})
case options
when Hash
if block_given?
locals[options[:layout]] ||= []
locals[options[:layout]] << options[:locals]
rendered_views.add options[:layout], options[:locals]
elsif options.key?(:partial)
locals[options[:partial]] ||= []
locals[options[:partial]] << options[:locals]
rendered_views.add options[:partial], options[:locals]
end
else
locals[options] ||= []
locals[options] << local_assigns
rendered_views.add options, local_assigns
end

super
Expand All @@ -183,7 +201,7 @@ def view
view = @controller.view_context
view.singleton_class.send :include, _helpers
view.extend(Locals)
view.locals = self.locals
view.rendered_views = self.rendered_views
view.output_buffer = self.output_buffer
view
end
Expand All @@ -200,7 +218,7 @@ def view
:@_routes,
:@controller,
:@_layouts,
:@_locals,
:@_rendered_views,
:@method_name,
:@output_buffer,
:@_partials,
Expand Down

0 comments on commit d6524d7

Please sign in to comment.