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

Adds a test mode for convenience in test suites #561

Merged
merged 1 commit into from Apr 22, 2015

Conversation

patbenatar
Copy link
Contributor

@behrad First pass at implementing something for #560. See README change for details on use. Thoughts?

@behrad
Copy link
Collaborator

behrad commented Apr 5, 2015

thank you for this @patbenatar
couldn't it be

mock = require('kue').createMock(); // or createMockQueue
// mock contains all Queue methods...

adding a test mode to existing Queue singleton complicates things... I prefer to have a mock and a queue object side by side :)

@patbenatar
Copy link
Contributor Author

@behrad My intention was for the code under test to not have to change in order to support this test mode—it continues to use the queue as it would at actual runtime. Adding the ability to create mock queues would work well if the system under test uses dependency injection everywhere, but how would it handle the case where you don't control the queue object that a system is using?

Consider this contrived example:

// thing.js
var queue = require('kue').createQueue();

module.exports = function() {
  queue.createJob('myJob');
};
// thing_test.js
var thing = require('./thing');

it('enqueues myJob', function() {
  thing();
  // expect job to be queued by inspecting the queue singleton
});

Compared to the dependency injected version:

// thing.js
module.exports = function(queue) {
  return function() {
    queue.createJob('myJob');
  };
};
// thing_test.js
var queue = require('kue').createMockQueue();
var thing = require('./thing')(queue);

it('enqueues myJob', function() {
  thing();
  // expect job to be queued by inspecting mock queue
});

The DI version would be pretty easy to inject a mock without any support from kue library, perhaps using something like sinon.

The non-DI version needs some manipulation of the queue singleton so that both the test suite and code under test are operating on the same object.

I hope that sheds some light into why I went this route.. in my current use case, the system under test is an existing system that does not use DI in a way that would accommodate injecting a mock queue. I tried to isolate all of the test stuff under queue.testMode and by providing enter and exit, test cases can individually decide to turn on/off the test mode, restoring the queue to its natural non-test state after the case completes.

@jdwolk
Copy link

jdwolk commented Apr 6, 2015

@patbenatar 👍 super handy!

@behrad
Copy link
Collaborator

behrad commented Apr 6, 2015

@patbenatar got your point, will return to this in days...

@patbenatar
Copy link
Contributor Author

@behrad Sounds good. We're using my fork in the meantime and I'll post back if we run into issues or improvements.

@behrad
Copy link
Collaborator

behrad commented Apr 6, 2015

👍 @patbenatar

behrad added a commit that referenced this pull request Apr 22, 2015
Adds a test mode for convenience in test suites
@behrad behrad merged commit 42248a5 into Automattic:master Apr 22, 2015
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

Successfully merging this pull request may close these issues.

None yet

3 participants