raweng / mock-kue

mock out kue
MIT License
6 stars 9 forks source link

Not sure how to use mock-kue #6

Open atlantageek opened 8 years ago

atlantageek commented 8 years ago

Trying to understand how to use mock-kue. The README says just require it. But I only want to use it during testing. Do I put an if statement around it or what? Or does mock-kue realize its running in a test environment and it doesnt look like it overrides the createQueue function. CreateQueue requires redis to be running on the PC. Is this expected.

rtodea commented 8 years ago

From looking at the source code, this does not mock a Redis connection. i.e. createQueue will still throw something like:

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
at Object.exports._errnoException (util.js:1026:11)
    at exports._exceptionWithHostPort (util.js:1049:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1081:14)

If you provide a connection, then mock-kue will wrap around the Job object. E.g.

// production-code.js
module.exports = {
  createJob: () => {
    require('kue')
      .createQueue()
      .create('some-job', {title: 'some-job-in-production'})
      .save();
  }
}
// test.js
const pc = require('./production-code');
require('mock-kue'); // it must be placed before calling `createJob`

pc.createJob(); // will not enqueue any job because of `mock-kue`
fruch commented 8 years ago

why can't it mock the redis connection as well ?