Skip to content

Commit

Permalink
Mock the Redis connection with the "mock_redis" gem. It allows Resque…
Browse files Browse the repository at this point in the history
… plugins not to rely on a real Redis server.
  • Loading branch information
nfo committed Oct 14, 2011
1 parent 7e48b8d commit 2a58973
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -3,11 +3,13 @@ PATH
specs:
resque_unit (0.4.0)
json (>= 1.4.6)
mock_redis

GEM
remote: http://rubygems.org/
specs:
json (1.5.3)
mock_redis (0.2.0)
rake (0.8.7)
shoulda (2.11.3)

Expand Down
1 change: 1 addition & 0 deletions lib/resque_unit.rb
Expand Up @@ -8,6 +8,7 @@ module ResqueUnit
end

require 'test/unit'
require 'mock_redis'
require 'resque_unit/helpers'
require 'resque_unit/resque'
require 'resque_unit/errors'
Expand Down
15 changes: 15 additions & 0 deletions lib/resque_unit/resque.rb
Expand Up @@ -4,6 +4,21 @@ module Resque
include Helpers
extend self

# When Resque tries to configure Redis, it just initialize an instance
# of MockRedis.
# Not meant to mock the main Resque features, just here for plugins.
def redis=(*args)
redis
end

# Returns the current Redis mock connection. If none has been created,
# will create a new one.
# Not meant to mock the main Resque features, just here for plugins.
def redis
@redis = MockRedis.new unless @redis.is_a?(MockRedis)
@redis ||= MockRedis.new
end

# Resets all the queues to the empty state. This should be called in
# your test's +setup+ method until I can figure out a way for it to
# automatically be called.
Expand Down
1 change: 1 addition & 0 deletions resque_unit.gemspec
Expand Up @@ -14,4 +14,5 @@ spec = Gem::Specification.new do |s|
s.files = Dir['lib/**/*.rb'] + Dir['test/**/*.rb']
s.test_files = Dir.glob('test/*_test.rb')

s.add_dependency "mock_redis" # For Resque plugins
end
11 changes: 11 additions & 0 deletions test/resque_unit_test.rb
Expand Up @@ -457,4 +457,15 @@ def setup
end
end

context "A plugin using Redis" do
should "use an instance of MockRedis" do
assert_instance_of MockRedis, Resque.redis
end

should "use an instance of MockRedis even if Resque tries to configure Redis" do
Resque.redis = 'lol'
assert_instance_of MockRedis, Resque.redis
end
end

end

0 comments on commit 2a58973

Please sign in to comment.