Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save readysetawesome/5273892 to your computer and use it in GitHub Desktop.
Save readysetawesome/5273892 to your computer and use it in GitHub Desktop.
I wanted to rework this using new syntax: "subset.should be_a_subset_of(superset, proc)" inspired by https://gist.github.com/cee-dub/623589 which was inspired by https://gist.github.com/zbrock/615961
def should_be_a_subset(superset, records_selected_by_scope, &condition)
flunk "Your superset is empty" if superset.empty?
flunk "Your scope did not select any records" if records_selected_by_scope.empty?
records_selected_by_block, records_excluded_by_block = superset.partition(&condition)
flunk "Your test condition did not select any records" if records_selected_by_block.empty?
flunk "Your test condition did not exclude any records" if records_excluded_by_block.empty?
records_selected_by_scope.map(&:id).should =~ records_selected_by_block.map(&:id)
end
# example
# User.have_logged_in.should be_a_subset_of(User.all, lambda {|u| u.last_logged_in_at.present? })
RSpec::Matchers.define :be_a_subset_of do |superset, condition|
match do |subset|
should_be_a_subset(superset, subset, &condition)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment