Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the mock extensible in a similar way as the real redis client #163

Open
kamiljano opened this issue Dec 13, 2020 · 0 comments
Open
Assignees

Comments

@kamiljano
Copy link
Collaborator

As discussed in #157 the redis-mock needs to be extensible via addCommand() and addMockCommand().

The addMockCommand() allows to define new logic for the mock in the following manner:

const mock = require('redis-mock');

mock.addMockCommand({
   'json.set': (client, args, callback) => {
      // your logic here... probably something like: client.set(client, args[0], JSON.stringify(args[1]), callback);
   }
   'json.get': (client, args, callback) => ...
});

None of the mocked functions are added to the mock client straight away though. They get attached to the client only after calling the addCommand() method, which matches the interface of the redis module. So in this case, the addCommand() would have to be called twice:

mock.addCommand('json.set');
mock.addCommand('json.get');

these 2 calls attach the predefined mock definitions into the mock client under 4 name variations:

  • mock.JSON_GET
  • mock.json_get
  • mock['json.get']
  • mock['JSON.GET']

Should the addCommand() be called with a parameter that does not have a mock definition, what should be linked to the attached method is a functions throwing an error stating "No mock definition provided for ${commandName}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant