Skip to content

Instantly share code, notes, and snippets.

@thumblemonks
Created December 30, 2009 23:07
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 thumblemonks/266492 to your computer and use it in GitHub Desktop.
Save thumblemonks/266492 to your computer and use it in GitHub Desktop.
# 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