Ioredis: A proposal for a mock module for ioredis

Created on 23 May 2015  路  16Comments  路  Source: luin/ioredis

mranney/node_redis has many mock libraries that help us to write unit tests without actually starting a Redis server.

Examples are:

https://github.com/faeldt/redis-mock
https://github.com/hdachev/fakeredis

Any plan to provide a mock interface?

Most helpful comment

Hey guys, I started working on it here: https://github.com/stipsan/ioredis-mock

All 16 comments

There's already a mock server used in the unit tests of ioredis. https://github.com/luin/ioredis/blob/master/test/helpers/mock_server.js

Maybe you should take another look at what I suggested.

In terms of unit testing, which you need is not a fake client that doing exactly the same thing(hopefully) as ioredis except leaving Redis server untouched, instead, you need a convenient way to stub calls to Redis server, which is mockServer doing. I've checked out mock libraries you suggested long ago and IMHO they are on the wrong tack.

Closing the issue. Feel free to reopen it with more detailed informations.

Can you post an example how to make a unit test using mock_server??
For example, how can i test a function which save in redis a hmset.
The function could receive two parameters, the client redis object and an object to save as hmset
How I test (Unit Test) that?
Thanks.

The problem I ran into with the mock_server is has to do with this line. It returns "OK" when trying to get a key that doesn't exist, which prevents me from using it in testing. For example:

ioredis.del('foo').then(function() {
  ioredis.get('foo').then(function(res) {
    // res === 'OK';
  });
});

The mock server isn't intent on being a redis emulator, instead, it's designed to make testing for ioredis easier. Refer to https://github.com/luin/ioredis/blob/master/test/functional/cluster.js for more use cases.

A simple example to test whether ioredis sends GET command to the redis server (mock server here) when calling .get() method:

var port = 17379;
it('should send GET command', function (done) {
  var redis = new Redis({ port: port });
  var server = new MockServer(port, function (argv) {
    if (argv[0] === 'get') {
      redis.disconnect();
      server.disconnect();
      done();
    }
  });
  redis.get('foo');
});

Ah, OK. I see now. Thank you for that clarification. It would be really nice if there were an equivalent of fakeredis that worked with ioredis. Or, is there a way to make that work that I'm not thinking of?

Not that I know of. As for me, mock_server is a good choice if you are writing unit tests since it's handy to test the interaction between your function and the redis server. For integration testing, running a real redis server is a wiser choice because emulators like fakeredis cannot 100% give you the same behaviour as real redis.

For many cases I agree running a real redis server is the wisest choice.
At the same time I think there are cases where the requirements of your project have different tradeoffs making an emulator a better choice.

Cases like:

  • Your workflow already use a local redis-server instance for the dev server.
  • You're on a platform without an official redis release, that's even worse than using an emulator.
  • You're running tests on a CI, setting it up is complicated. If you combine it with CI that also run selenium acceptance testing it's even more complicated, as two redis-server instances on the same CI build is hard.
  • The GitHub repo have bots that run the testing suite and is limited through npm package.json install scripts and can't fire up servers. (Having Greenkeeper notifying me when a new release of ioredis is out and wether my code breaks or not is awesome).

I've started writing a mock-ioredis utility for one of my projects. So far I've covered just the methods that I'm using in a personal project:

  • incr

    • hsetnx

    • hmset

    • sadd

    • srem

    • hget

    • hvals

    • hgetall

    • smembers

    • sismember

    • hset

    • multi

    • exec

I'm going to put it up on github and npm so that those of us who need it can collab on it, and the readme will make it clear that it's only meant to solve use cases where testing against a real redis-server is impractical.

Hey guys, I started working on it here: https://github.com/stipsan/ioredis-mock

@stipsan looks promising!

Awesome @stipsan !!

Thanks guys!

I've added a compat.md that shows the status of implemented commands.

Should we add a reference to it here: https://github.com/luin/ioredis#running-tests so that people can find it?

@stipsan Sounds good. Would you like to make a pull request for this?

Sure thing!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lakamsani picture lakamsani  路  3Comments

PhillipWinder picture PhillipWinder  路  3Comments

saschaishikawa picture saschaishikawa  路  4Comments

ORESoftware picture ORESoftware  路  5Comments

haoxins picture haoxins  路  5Comments