rubycachingrspecdalli

Rspec: testing dalli cache (outside of rails)


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

Solution

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

    That's all pretty straightforward with RSpec