Skip to content

Instantly share code, notes, and snippets.

@Tonkonozhenko
Forked from mlanett/rspec_helper.rb
Created January 30, 2017 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tonkonozhenko/53f56c7edd0832506a71eec6f684d75f to your computer and use it in GitHub Desktop.
Save Tonkonozhenko/53f56c7edd0832506a71eec6f684d75f to your computer and use it in GitHub Desktop.
Helper to clear redis before/after examples in rspec.
=begin
Include in your rspec config like so:
RSpec.configure do |spec|
spec.include RSpec::RedisHelper, redis: true
end
This helper will clean redis around each example.
=end
module RSpec
module RedisHelper
# When this module is included into the rspec config,
# it will set up an around(:each) block to clear redis.
def self.included(rspec)
rspec.around(:each, redis: true) do |example|
with_clean_redis do
example.run
end
end
end
CONFIG = { url: ENV["REDIS_URL"] || "redis://127.0.0.1:6379/1" }
def redis(&block)
@redis ||= ::Redis.connect(CONFIG)
end
def with_clean_redis(&block)
redis.flushall # clean before run
begin
yield
ensure
redis.flushall # clean up after run
end
end
end # RedisHelper
end # RSpec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment