Created
August 16, 2016 09:21
-
-
Save jameslafa/91b75d7da7f00aacc1b9764e442c7415 to your computer and use it in GitHub Desktop.
RSpec::Matchers have_same_attributes_as
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
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