Created
December 30, 2009 23:07
-
-
Save thumblemonks/266492 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# KEEPING HERE FOR POSTERITY (_gus) | |
# Allows for a named_scope chain like this, | |
# Business.owned_by(current_user).limit(10) | |
# to be cleanly tested like this, | |
# assert_named_scope_chain(Business, { :owned_by => current_user }, { :limit => 10 }) | |
# rather than having to create nasty mock objects. | |
def expected_named_scope_chain(*ordered_chain) | |
results = [] | |
ordered_chain.reverse.each_with_index do |named_scope_link, i| | |
last = i == (ordered_chain.size - 2) | |
link = last ? ordered_chain.first : mock() | |
if named_scope_link.is_a? Hash # e.g. Business.limit(10) aka { :limit => 10 } | |
expectation = named_scope_link.first.first # e.g. :limit | |
param = named_scope_link.first.last # e.g. 10 | |
link.expects(expectation).with(param).returns(results) | |
else # e.g. Business.claimed aka :claimed | |
expectation = named_scope_link # e.g. :claimed | |
link.expects(expectation).returns(results) | |
end | |
break if last | |
results = link | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment