Skip to content

Instantly share code, notes, and snippets.

@janmejay
Created December 4, 2008 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janmejay/31865 to your computer and use it in GitHub Desktop.
Save janmejay/31865 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment