Created
May 11, 2013 22:38
-
-
Save nowk/5561666 to your computer and use it in GitHub Desktop.
RSpec cache matcher
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
RSpec::Matchers.define :have_key do |expected| | |
def value | |
@value | |
end | |
def cache_record | |
@cache_record | |
end | |
def has_cache_key? | |
cache_record != nil | |
end | |
def matches_value? | |
return true unless value.present? | |
cache_record == value | |
end | |
match do |actual| | |
@cache_record = actual.read(expected) | |
has_cache_key? && matches_value? | |
end | |
chain :with_value do |value| | |
@value = value | |
end | |
failure_message_for_should do |actual| | |
case | |
when !has_cache_key? | |
"expected to have a cache with key: '#{expected}', but did not" | |
when !matches_value? | |
"expected cache value for: '#{expected}' to be #{value}, but was #{cache_record}" | |
end | |
end | |
failure_message_for_should_not do |actual| | |
case | |
when has_cache_key? | |
"expected not to have a cache with key: '#{expected}', but did" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment