Rspec: testing dalli cache (outside of rails)

299 views Asked by At

This might be outside the realm of what you should test, but how could one test a service method that uses caching (i.e. that it uses the cache):

def get_payment_methods(country_code, filter=[])
    key = "/countries/#{country_code.upcase}/payment_methods"
    payment_methods = CACHE.get(key)
    unless payment_methods
      payment_methods = My::Service.get_payment_methods_for_country(country_code.upcase).first
      CACHE.set(key, payment_methods)
    end
    # ...
end
1

There are 1 answers

0
Peter Alfvin On BEST ANSWER

You could create a CACHE double and verify the following for your method:

  • It calls the cache
  • It uses the cache value and doesn't get the payment method if the cached value is present
  • It gets the payment and saves the cache value if the cached value is not present

That's all pretty straightforward with RSpec