Skip to content

Commit

Permalink
Support testing by mocking MockRedis#evalsha
Browse files Browse the repository at this point in the history
  • Loading branch information
vu-hoang-kaligo committed Jan 13, 2025
1 parent afbd363 commit a8e4b1e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,14 @@ end

# Render your response
```

### Testing

For those using `mock_redis` gem, some methods that `idempotency` gem uses are not implemented (e.g. eval, evalsha), and this could cause test cases to fail. To get around this, the gem has a monkeypatch over `mock_redis` gem to override the missing methods. To use it, simply add following lines to your `spec_helper.rb`:

```ruby
RSpec.configure do |config|
config.include Idempotency::Testing::Helpers
end
```

29 changes: 29 additions & 0 deletions lib/idempotency/testing/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'idempotency/cache'

class Idempotency
module Testing
module Helpers
def self.included(_base)
return unless defined?(MockRedis)

MockRedis.class_eval do
def evalsha(sha, keys:, argv:)
return unless sha == Idempotency::Cache::COMPARE_AND_DEL_SCRIPT_SHA

value = argv[0]
cached_value = get(keys[0])

if value == cached_value
del(keys[0])
value
else
cached_value
end
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/idempotency/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class Idempotency
VERSION = '0.1.2'
VERSION = '0.1.3'
end

0 comments on commit a8e4b1e

Please sign in to comment.