janmejay (owner)

Revisions

gist: 31865 Download_button fork
public
Public Clone URL: git://gist.github.com/31865.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
ActionView::Base.class_eval do
  $render_expectation_matcher_loaded = true
  
  def set_render_expectation options_map
    options_map.reverse_merge!(:with => [], :times => 1, :return => nil)
    _get_expectation_map[options_map[:with]] = options_map.except(:with).merge(:expected_times => options_map[:times])
  end
 
  def _get_expectation_map
    self.instance_variable_get('@render_expectations') || self.instance_variable_set('@render_expectations', {})
  end
 
  def render_with_expectation_matcher *args
    expectation = _get_expectation_map[_get_expectation_map.keys.find {|key| key == args }] #hashcode of identical hashes is differnet. Comparision by identity is not allowed... if we upgrade to 1.9... someone delete this.... -jj
    return render_without_expectation_matcher(*args) unless expectation
    expectation[:times] -= 1
    return expectation[:return]
  end
 
  def assert_render_expectation_satisfied!
    _get_expectation_map.each do |with, expectation|
      (expectation[:times] == 0) && next
      raise "Render expectation with arguments='#{with.inspect}' didn't pass." +
            "It was supposed to be called #{expectation[:expected_times]} time(s), but was called #{expectation[:expected_times] - expectation[:times]} time(s)."
    end
  end
 
  alias_method_chain :render, :expectation_matcher
end unless $render_expectation_matcher_loaded