Created
September 22, 2014 04:23
-
-
Save mlanett/6b6f814ad3de178289a6 to your computer and use it in GitHub Desktop.
Helper to clear redis before/after examples in rspec.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=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