Skip to content

Instantly share code, notes, and snippets.

@nowk
Created May 11, 2013 22:38
Show Gist options
  • Save nowk/5561666 to your computer and use it in GitHub Desktop.
Save nowk/5561666 to your computer and use it in GitHub Desktop.
RSpec cache matcher
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