Skip to content

Instantly share code, notes, and snippets.

@jameslafa
Created August 16, 2016 09:21
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 jameslafa/91b75d7da7f00aacc1b9764e442c7415 to your computer and use it in GitHub Desktop.
Save jameslafa/91b75d7da7f00aacc1b9764e442c7415 to your computer and use it in GitHub Desktop.
RSpec::Matchers have_same_attributes_as
RSpec::Matchers.define :have_same_attributes_as do |expected|
ignored_attributes = ['id', 'updated_at', 'created_at']
match do |actual|
actual.attributes.except(*ignored_attributes) == expected.attributes.except(*ignored_attributes)
end
chain :except do |*attributes|
ignored_attributes += attributes.map {|a| a.to_s}
end
end
# Usage:
# expect(patient).to have_same_attributes_as(patient_copy)
# It compares every attributes of the 2 models without taking ['id', 'updated_at', 'created_at'] into account
#
# expect(patient).to have_same_attributes_as(patient_copy).except(:latitude, :longitude)
# It compares every attributes of the 2 models without taking ['id', 'updated_at', 'created_at', 'latitude', 'longitude'] into account
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment