1.0.1 • Published 10 years ago
m-stub v1.0.1
Stub
Installation
npm install stub [--save-dev]
Usage
Require the module:
var stub = require('stub');
The stub
object now has two public methods available:
stub.inject(modulePath, ...dependencyPaths)
Requires a module and stubs its dependencies.
Example:
foo.js:
var bar = require('bar');
module.exports = {
foo: function () {
return 'foo' + bar.print();
}
}
bar.js:
module.exports = {
print: function () {
return 'bar';
}
};
foo.test.js:
var stub = require('stub');
var fooWithStubbedBar = stub.inject('./foo', './bar');
/* Use fooWithStubbedBar for testing... */
stub.get(module, dependency , method)
Returns the stub of a module dependency. Optionally, you can provide the stubbed method name as an argument to get access to the stubbed method directly.
Example (with the same modules as the example above):
foo.test.js:
var stub = require('stub');
var fooWithStubbedBar = stub.inject('./foo', './bar');
/* Get entire stub: */
var stubbedBar = stub.get(fooWithStubbedBar, './bar');
/* Or get stubbed method: */
var stubbedBarPrintMethod = stub.get(fooWithStubbedBar, './bar', 'print');