sds / mock_redis

Mock Redis gem for Ruby
Other
493 stars 157 forks source link

MockRedis

Gem Version Build Status Coverage Status

MockRedis provides the same interface as redis-rb, but it stores its data in memory instead of talking to a Redis server. It is intended for use in tests.

Requirements

Ruby 3.0+

The current implementation is tested against Redis 6.2. Older versions may work, but can also return different results or not support some commands.

Getting Started

Install the gem:

gem install mock_redis

It's as easy as require 'mock_redis'; mr = MockRedis.new. Then you can call the same methods on it as you can call on a real Redis object.

For example:

require 'mock_redis'
mr = MockRedis.new
mr.set('some key', 'some value') # => "OK"
mr.get('some key') # => "some value"

Supported Features

mock_redis supports most of the methods that redis-rb does. Examples of supported methods:

Mostly-Supported Commands

A MockRedis object can't do everything that a real Redis client can since it's an in-memory object confined to a single process. MockRedis makes every attempt to be Redis-compatible, but there are some necessary exceptions.

Unsupported Commands

Some stuff, we just can't do with a single Ruby object in a single Ruby process.

Remaining Work

There are some things we want to have in here, but that we just haven't gotten to doing yet. If you're interested in helping out, please submit a pull request with your (tested!) implementation.

Running tests

We recommend running Redis within a Docker container to make development as simple as possible, but as long as you have a Redis instance listening on localhost:6379 you should be good to go.

  1. Start Redis.
    docker run --rm -p 6379:6379 redis:6.2-alpine
  2. Install dependencies.
    bundle install
  3. Run tests.
    bundle exec rspec

These tests were written with Redis running on localhost without any passwords required. If you're using a different version of Redis, you may see failures due to error message text being different and other breaking changes over time.

Changelog

If you're interested in seeing the changes and bug fixes between each version of mock_redis, read the MockRedis Changelog.

License

This project is released under the MIT license.