Skip to content

Instantly share code, notes, and snippets.

@mpan-wework
Last active December 20, 2018 02:36
Show Gist options
  • Save mpan-wework/304118cc12b055ad0b323b0ea802706c to your computer and use it in GitHub Desktop.
Save mpan-wework/304118cc12b055ad0b323b0ea802706c to your computer and use it in GitHub Desktop.
Fake redis in rspec

Rails cache:

The main methods to call are clear, read, write, delete, exist?, and fetch.

class FakeRedis
  def initialize; @data = {}; end

  def read(key, *args) @data[key] end
  def write(key, val, *args) @data[key] = val end
  def delete(key) @data.delete(key) end
  def exists?(key) @data.include?(key) end
  def fetch(key, *args) @data[key] ||= yield if block_given? end
  def clear; @data = {}; end
end
allow(Rails).to receive(:cache).and_return(FakeRedis.new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment