{"name":"gently","version":"0.5.0","directories":{"lib":"./lib/gently"},"main":"./lib/gently/index","_id":"gently@0.5.0","engines":{"node":"*"},"_nodeSupported":true,"_npmVersion":"0.2.7-2","_nodeVersion":"v0.3.1-pre","dist":{"tarball":"http://registry-npm.ywwl.com/gently/download/gently-0.5.0.tgz","shasum":"90a7f88cdf34d7c8057dcf7208776672d9d53f2c","integrity":"sha512-yyhXgq0iYW26kGt9VmgBrJy9wwXY76nawAhVTOp1/NVQPGm27uGuOeMfA9XIGoZXhdHmC/hvTqMfEHWOETsiyg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIE8zMdQfhXX6lYHXbdo1QWSAmVGaN44LrFXlxwSAw66qAiAyr5SdoXtQFnes5skJLc9EyaIXU77YXk0OPPPs5S08Dw=="}],"size":4301,"noattachment":false,"key":"/gently/-/gently-0.5.0.tgz"},"readme":"# Gently\n\n<!-- badges/ -->\n[![Build Status](https://travis-ci.org/felixge/node-gently.svg?branch=master)](https://travis-ci.org/felixge/node-gently)\n[![npm](https://img.shields.io/npm/v/gently.svg)](https://www.npmjs.com/package/gently) \n[![Dependency Status](https://david-dm.org/felixge/node-gently.svg?theme=shields.io)](https://david-dm.org/felixge/node-gently)\n[![Development Dependency Status](https://david-dm.org/felixge/node-gently/dev-status.svg?theme=shields.io)](https://david-dm.org/felixge/node-gently#info=devDependencies)\n<!-- /badges -->\n\n## Purpose\n\nA node.js module that helps with stubbing and behavior verification. It allows you to test the most remote and nested corners of your code while keeping being fully unobtrusive.\n\n## Features\n\n* Overwrite and stub individual object functions\n* Verify that all expected calls have been made in the expected order\n* Restore stubbed functions to their original behavior\n* Detect object / class names from obj.constructor.name and obj.toString()\n* Hijack any required module function or class constructor\n\n## Installation\n\nVia [npm](http://github.com/isaacs/npm):\n\n    npm install gently@latest\n\n## Example\n\nMake sure your dog is working properly:\n```javascript\nfunction Dog() {}\n\nDog.prototype.seeCat = function() {\n  this.bark('whuf, whuf');\n  this.run();\n}\n\nDog.prototype.bark = function(bark) {\n  require('sys').puts(bark);\n}\n\nconst gently = new (require('gently'))\n  , assert = require('assert')\n  , dog = new Dog();\n\ngently.expect(dog, 'bark', function(bark) {\n  assert.equal(bark, 'whuf, whuf');\n});\ngently.expect(dog, 'run');\n\ndog.seeCat();\n```\nYou can also easily test event emitters with this, for example a simple sequence of 2 events emitted by `fs.WriteStream`:\n```javascript\nconst gently = new (require('gently'))\n  , stream = new (require('fs').WriteStream)('my_file.txt');\n\ngently.expect(stream, 'emit', function(event) {\n  assert.equal(event, 'open');\n});\n\ngently.expect(stream, 'emit', function(event) {\n  assert.equal(event, 'drain');\n});\n```\nFor a full read world example, check out this test case: [test-incoming-form.js](http://github.com/felixge/node-formidable/blob/master/test/simple/test-incoming-form.js) (in [node-formdiable](http://github.com/felixge/node-formidable)).\n\n## API\n\n### Gently\n\n#### new Gently()\n\nCreates a new gently instance. It listens to the process `'exit'` event to make sure all expectations have been verified.\n\n#### gently.expect(obj, method, [[count], stubFn])\n\nCreates an expectation for an objects method to be called. You can optionally specify the call `count` you are expecting, as well as `stubFn` function that will run instead of the original function.\n\nReturns a reference to the function that is getting overwritten.\n\n#### gently.expect([count], stubFn)\n\nReturns a function that is supposed to be executed `count` times, delegating any calls to the provided `stubFn` function. Naming your stubFn closure will help to properly diagnose errors that are being thrown:\n```javascript\nchildProcess.exec('ls', gently.expect(function lsCallback(code) {\n  assert.equal(0, code);\n}));\n```\n#### gently.restore(obj, method)\n\nRestores an object method that has been previously overwritten using `gently.expect()`.\n\n#### gently.hijack(realRequire)\n\nReturns a new require functions that catches a reference to all required modules into `gently.hijacked`.\n\nTo use this function, include a line like this in your `'my-module.js'`.\n```javascript\nif (global.GENTLY) require = GENTLY.hijack(require);\n\nconst sys = require('sys');\nexports.hello = function() {\n  sys.log('world');\n};\n```\nNow you can write a test for the module above:\n```javascript\nconst gently = global.GENTLY = new (require('gently'))\n  , myModule = require('./my-module');\n\ngently.expect(gently.hijacked.sys, 'log', function(str) {\n  assert.equal(str, 'world');\n});\n\nmyModule.hello();\n```\n#### gently.stub(location, [exportsName])\n\nReturns a stub class that will be used instead of the real class from the module at `location` with the given `exportsName`.\n\nThis allows to test an OOP version of the previous example, where `'my-module.js'`.\n```javascript\nif (global.GENTLY) require = GENTLY.hijack(require);\n\nconst World = require('./world');\n\nexports.hello = function() {\n  const world = new World();\n  world.hello();\n}\n```\nAnd `world.js` looks like this:\n```javascript\nconst sys = require('sys');\n\nfunction World() {\n\n}\nmodule.exports = World;\n\nWorld.prototype.hello = function() {\n  sys.log('world');\n};\n```\nTesting `'my-module.js'` can now easily be accomplished:\n```javascript\nconst gently = global.GENTLY = new (require('gently'))\n  , WorldStub = gently.stub('./world')\n  , myModule = require('./my-module')\n  , WORLD;\n\ngently.expect(WorldStub, 'new', function() {\n  WORLD = this;\n});\n\ngently.expect(WORLD, 'hello');\n\nmyModule.hello();\n```\n#### gently.hijacked\n\nAn object that holds the references to all hijacked modules.\n\n#### gently.verify([msg])\n\nVerifies that all expectations of this gently instance have been satisfied. If not called manually, this method is called when the process `'exit'` event is fired.\n\nIf `msg` is given, it will appear in any error that might be thrown.\n\n## License\n\nGently is licensed under the MIT license.\n","publish_time":1304441123222,"maintainers":[{"email":"felix@debuggable.com","name":"felixge"},{"email":"kevin@vanzonneveld.net","name":"kvz"}],"_cnpm_publish_time":1304441123222,"dist-tags":{"latest":"1.0.0"}}