Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add markdown syntax highlighting #15

Merged
merged 1 commit into from

2 participants

@ulikoehler
Collaborator

This improves the documentation readability.

@felixge felixge merged commit 3bc74eb into from
@felixge
Owner

Thanks / merged. Not really using this lib anymore, so I added you as a collaborator in case you'd like to make changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 22, 2012
  1. @ulikoehler
This page is out of date. Refresh to see the latest.
Showing with 80 additions and 80 deletions.
  1. +80 −80 Readme.md
View
160 Readme.md
@@ -21,42 +21,42 @@ Via [npm](http://github.com/isaacs/npm):
## Example
Make sure your dog is working properly:
+```javascript
+function Dog() {}
- function Dog() {}
+Dog.prototype.seeCat = function() {
+ this.bark('whuf, whuf');
+ this.run();
+}
- Dog.prototype.seeCat = function() {
- this.bark('whuf, whuf');
- this.run();
- }
+Dog.prototype.bark = function(bark) {
+ require('sys').puts(bark);
+}
- Dog.prototype.bark = function(bark) {
- require('sys').puts(bark);
- }
+var gently = new (require('gently'))
+ , assert = require('assert')
+ , dog = new Dog();
- var gently = new (require('gently'))
- , assert = require('assert')
- , dog = new Dog();
-
- gently.expect(dog, 'bark', function(bark) {
- assert.equal(bark, 'whuf, whuf');
- });
- gently.expect(dog, 'run');
-
- dog.seeCat();
+gently.expect(dog, 'bark', function(bark) {
+ assert.equal(bark, 'whuf, whuf');
+});
+gently.expect(dog, 'run');
+dog.seeCat();
+```
You can also easily test event emitters with this, for example a simple sequence of 2 events emitted by `fs.WriteStream`:
-
- var gently = new (require('gently'))
- , stream = new (require('fs').WriteStream)('my_file.txt');
-
- gently.expect(stream, 'emit', function(event) {
- assert.equal(event, 'open');
- });
-
- gently.expect(stream, 'emit', function(event) {
- assert.equal(event, 'drain');
- });
-
+```javascript
+var gently = new (require('gently'))
+ , stream = new (require('fs').WriteStream)('my_file.txt');
+
+gently.expect(stream, 'emit', function(event) {
+ assert.equal(event, 'open');
+});
+
+gently.expect(stream, 'emit', function(event) {
+ assert.equal(event, 'drain');
+});
+```
For 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)).
## API
@@ -76,11 +76,11 @@ Returns a reference to the function that is getting overwritten.
#### gently.expect([count], stubFn)
Returns 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:
-
- childProcess.exec('ls', gently.expect(function lsCallback(code) {
- assert.equal(0, code);
- }));
-
+```javascript
+childProcess.exec('ls', gently.expect(function lsCallback(code) {
+ assert.equal(0, code);
+}));
+```
#### gently.restore(obj, method)
Restores an object method that has been previously overwritten using `gently.expect()`.
@@ -90,68 +90,68 @@ Restores an object method that has been previously overwritten using `gently.exp
Returns a new require functions that catches a reference to all required modules into `gently.hijacked`.
To use this function, include a line like this in your `'my-module.js'`.
-
- if (global.GENTLY) require = GENTLY.hijack(require);
-
- var sys = require('sys');
- exports.hello = function() {
- sys.log('world');
- };
-
+```javascript
+if (global.GENTLY) require = GENTLY.hijack(require);
+
+var sys = require('sys');
+exports.hello = function() {
+ sys.log('world');
+};
+```
Now you can write a test for the module above:
+```javascript
+var gently = global.GENTLY = new (require('gently'))
+ , myModule = require('./my-module');
- var gently = global.GENTLY = new (require('gently'))
- , myModule = require('./my-module');
-
- gently.expect(gently.hijacked.sys, 'log', function(str) {
- assert.equal(str, 'world');
- });
-
- myModule.hello();
+gently.expect(gently.hijacked.sys, 'log', function(str) {
+ assert.equal(str, 'world');
+});
+myModule.hello();
+```
#### gently.stub(location, [exportsName])
Returns a stub class that will be used instead of the real class from the module at `location` with the given `exportsName`.
This allows to test an OOP version of the previous example, where `'my-module.js'`.
+```javascript
+if (global.GENTLY) require = GENTLY.hijack(require);
- if (global.GENTLY) require = GENTLY.hijack(require);
-
- var World = require('./world');
-
- exports.hello = function() {
- var world = new World();
- world.hello();
- }
+var World = require('./world');
+exports.hello = function() {
+ var world = new World();
+ world.hello();
+}
+```
And `world.js` looks like this:
+```javascript
+var sys = require('sys');
- var sys = require('sys');
+function World() {
- function World() {
-
- }
- module.exports = World;
-
- World.prototype.hello = function() {
- sys.log('world');
- };
+}
+module.exports = World;
+World.prototype.hello = function() {
+ sys.log('world');
+};
+```
Testing `'my-module.js'` can now easily be accomplished:
+```javascript
+var gently = global.GENTLY = new (require('gently'))
+ , WorldStub = gently.stub('./world')
+ , myModule = require('./my-module')
+ , WORLD;
- var gently = global.GENTLY = new (require('gently'))
- , WorldStub = gently.stub('./world')
- , myModule = require('./my-module')
- , WORLD;
-
- gently.expect(WorldStub, 'new', function() {
- WORLD = this;
- });
-
- gently.expect(WORLD, 'hello');
+gently.expect(WorldStub, 'new', function() {
+ WORLD = this;
+});
- myModule.hello();
+gently.expect(WORLD, 'hello');
+myModule.hello();
+```
#### gently.hijacked
An object that holds the references to all hijacked modules.
@@ -164,4 +164,4 @@ If `msg` is given, it will appear in any error that might be thrown.
## License
-Gently is licensed under the MIT license.
+Gently is licensed under the MIT license.
Something went wrong with that request. Please try again.