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?
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:
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:
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!
Most helpful comment
Hey guys, I started working on it here: https://github.com/stipsan/ioredis-mock