Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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])
def _get_expectation_map
self.instance_variable_get('@render_expectations') || self.instance_variable_set('@render_expectations', {})
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]
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)."
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