Created
June 6, 2020 00:05
-
-
Save indyarocks/067702297b99bf8dfbee3ef60ebf89e1 to your computer and use it in GitHub Desktop.
Rspec matcher for attr_accessor, attr_reader and attr_writer
This file contains hidden or 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
# have_attr_reader matcher for attr_reader | |
RSpec::Matchers.define :have_attr_reader do |field| | |
match do |object_instance| | |
object_instance.respond_to?(field) | |
end | |
failure_message do |object_instance| | |
"expected attr_reader for #{field} on #{object_instance}" | |
end | |
failure_message_when_negated do |object_instance| | |
"expected attr_reader for #{field} not to be defined on #{object_instance}" | |
end | |
description do |object_instance| | |
"have attr_reader for #{field} is defined on the instance of #{object_instance.class}." | |
end | |
end | |
# have_attr_writer matcher for attr_writer | |
RSpec::Matchers.define :have_attr_writer do |field| | |
match do |object_instance| | |
object_instance.respond_to?("#{field}=") | |
end | |
failure_message do |object_instance| | |
"expected attr_writer for #{field} on #{object_instance}" | |
end | |
failure_message_when_negated do |object_instance| | |
"expected attr_writer for #{field} not to be defined on #{object_instance}" | |
end | |
description do |object_instance| | |
"have attr_writer for #{field} is defined on the instance of #{object_instance.class}." | |
end | |
end | |
# `have_attr_accessor` matcher for attr_accessor | |
RSpec::Matchers.define :have_attr_accessor do |field| | |
match do |object_instance| | |
object_instance.respond_to?(field) && object_instance.respond_to?("#{field}=") | |
end | |
failure_message do |object_instance| | |
"expected attr_accessor for #{field} on #{object_instance}" | |
end | |
failure_message_when_negated do |object_instance| | |
"expected attr_accessor for #{field} not to be defined on #{object_instance}" | |
end | |
description do |object_instance| | |
"have attr_accessor for #{field} is defined on the instance of #{object_instance.class}." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment