Skip to content

Instantly share code, notes, and snippets.

@mrzasa
Created September 15, 2016 19:57
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 mrzasa/eb10f4523650ae07868e93b9169f71d1 to your computer and use it in GitHub Desktop.
Save mrzasa/eb10f4523650ae07868e93b9169f71d1 to your computer and use it in GitHub Desktop.
# This file contains shared examples that helps testing classes that has
# finder methods (e.g. ActiveRecord model classes).
#
# It accepts :expected, :others and :results params
# passed with let blocks:
#
# it_behaves_like :finder do
# let(:results) { ... }
# let(:expected) { ... }
# let(:others) { ... }
# end
#
# The exaples checks that all the expected records are returned by the finder
# and none of the others is returned.
#
shared_examples_for :finder do |condition_description, clazz|
@recods_string = clazz.blank? ? "records" : clazz.to_s.tableize
if !condition_description.blank?
@positive_example_name = "finds #{@records_string} that #{condition_description}"
else
@positive_example_name = "finds expected #{@records_string}"
end
it "#{@positive_example_name}" do
expect(results).to include(*expected)
end
it "does not find others" do
expect(results).not_to include(*others)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment