Comparing version 0.2.1 to 1.0.0
1.0.0 / 2014-10-30 | ||
================== | ||
* docs(readme): add badges | ||
* feat(error): support mock error on generator function | ||
* feat(data): support mock generator function | ||
0.2.1 / 2014-03-14 | ||
@@ -3,0 +10,0 @@ ================== |
@@ -1,1 +0,7 @@ | ||
module.exports = require('./lib/mm'); | ||
module.exports = require('./lib/mm'); | ||
var enable = require('enable'); | ||
if (enable.generator) { | ||
/* yup, ES6 */ | ||
require('./lib/es6'); | ||
} |
/**! | ||
* mm - lib/mm.js | ||
* | ||
* Copyright(c) 2012 - 2013 fengmk2 <fengmk2@gmail.com> | ||
* Copyright(c) fengmk2 and other contributors. | ||
* MIT Licensed | ||
* | ||
* Authors: | ||
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com) | ||
*/ | ||
"use strict"; | ||
'use strict'; | ||
@@ -20,3 +23,2 @@ /** | ||
var semver = require('semver'); | ||
var thunkify = require('thunkify'); | ||
@@ -71,3 +73,3 @@ var mock = module.exports = function mock(obj, key, method) { | ||
timeout = timeout || 0; | ||
mock(mod, method, thunkify(function () { | ||
mock(mod, method, function () { | ||
var callback = getCallback(arguments); | ||
@@ -77,3 +79,3 @@ setTimeout(function () { | ||
}, timeout); | ||
})); | ||
}); | ||
return this; | ||
@@ -98,8 +100,8 @@ }; | ||
} | ||
mock(mod, method, thunkify(function () { | ||
mock(mod, method, function () { | ||
var callback = getCallback(arguments); | ||
setTimeout(function () { | ||
callback.apply(null, [null].concat(datas)); | ||
callback.apply(mod, [null].concat(datas)); | ||
}, timeout); | ||
})); | ||
}); | ||
return this; | ||
@@ -106,0 +108,0 @@ }; |
{ | ||
"name": "mm", | ||
"version": "0.2.1", | ||
"version": "1.0.0", | ||
"description": "mock mate, mock http request, fs access and so on.", | ||
@@ -18,5 +18,7 @@ "main": "index.js", | ||
"dependencies": { | ||
"muk": "0.3.1", | ||
"semver": "2.2.1", | ||
"thunkify": "0.0.1" | ||
"co-sleep": "~0.0.1", | ||
"enable": "~1.0.0", | ||
"is-type-of": "~0.3.1", | ||
"muk": "~0.3.1", | ||
"semver": "~4.1.0" | ||
}, | ||
@@ -26,5 +28,6 @@ "devDependencies": { | ||
"blanket": "*", | ||
"chunkstream": "0.0.1", | ||
"co": "3.0.4", | ||
"co-urllib": "0.1.2", | ||
"chunkstream": "~0.0.1", | ||
"co": "~3.1.0", | ||
"co-mocha": "*", | ||
"co-urllib": "~0.2.3", | ||
"contributors": "*", | ||
@@ -35,4 +38,5 @@ "coveralls": "*", | ||
"node-patch": "*", | ||
"pedding": "0.0.3", | ||
"should": "3.1.3", | ||
"pedding": "~1.0.0", | ||
"should": "~4.1.0", | ||
"should-http": "*", | ||
"travis-cov": "*" | ||
@@ -39,0 +43,0 @@ }, |
234
README.md
@@ -1,11 +0,30 @@ | ||
mm (美眉,Mock伴侣) [![Build Status](https://secure.travis-ci.org/fengmk2/mm.png)](http://travis-ci.org/fengmk2/mm) [![Coverage Status](https://coveralls.io/repos/fengmk2/mm/badge.png)](https://coveralls.io/r/fengmk2/mm) | ||
mm | ||
======= | ||
[![NPM](https://nodei.co/npm/mm.png?downloads=true&stars=true)](https://nodei.co/npm/mm) | ||
[![NPM version][npm-image]][npm-url] | ||
[![build status][travis-image]][travis-url] | ||
[![Test coverage][coveralls-image]][coveralls-url] | ||
[![Gittip][gittip-image]][gittip-url] | ||
[![David deps][david-image]][david-url] | ||
[![node version][node-image]][node-url] | ||
[![npm download][download-image]][download-url] | ||
![logo](https://raw.github.com/fengmk2/mm/master/logo.png) | ||
[npm-image]: https://img.shields.io/npm/v/mm.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/mm | ||
[travis-image]: https://img.shields.io/travis/node-modules/mm.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/node-modules/mm | ||
[coveralls-image]: https://img.shields.io/coveralls/node-modules/mm.svg?style=flat-square | ||
[coveralls-url]: https://coveralls.io/r/node-modules/mm?branch=master | ||
[gittip-image]: https://img.shields.io/gittip/fengmk2.svg?style=flat-square | ||
[gittip-url]: https://www.gittip.com/fengmk2/ | ||
[david-image]: https://img.shields.io/david/node-modules/mm.svg?style=flat-square | ||
[david-url]: https://david-dm.org/node-modules/mm | ||
[node-image]: https://img.shields.io/badge/node.js-%3E=_0.10-green.svg?style=flat-square | ||
[node-url]: http://nodejs.org/download/ | ||
[download-image]: https://img.shields.io/npm/dm/mm.svg?style=flat-square | ||
[download-url]: https://npmjs.org/package/mm | ||
mock mate, easy to mock `http` request, `fs` access and so on. | ||
An simple but flexible **mock(or say stub)** package, mock mate. | ||
Mock伴侣,单元测试必备。 | ||
![logo](https://raw.github.com/node-modules/mm/master/logo.png) | ||
@@ -20,6 +39,106 @@ ## Install | ||
### Mock `http(s).request()` | ||
```js | ||
var mm = require('mm'); | ||
var fs = require('fs'); | ||
mm(fs, 'readFileSync', function (filename) { | ||
return filename + ' content'; | ||
}); | ||
console.log(fs.readFileSync('《九评 Java》')); | ||
// => 《九评 Java》 content | ||
mm.restore(); | ||
console.log(fs.readFileSync('《九评 Java》')); | ||
// => throw `Error: ENOENT, no such file or directory '《九评 Java》` | ||
``` | ||
### Support generator function | ||
```js | ||
var foo = { | ||
get: function* () { | ||
return 1; | ||
} | ||
}; | ||
mm.data(foo, 'get', 2); | ||
var data = yield* foo.get(); // data should return 2 | ||
mm.error(foo, 'get', 'error boom'); | ||
yield* foo.get(); // should throw error | ||
``` | ||
## API | ||
### .error(module, propertyName, errerMessage) | ||
```js | ||
var mm = require('mm'); | ||
var fs = require('fs'); | ||
mm.error(fs, 'readFile', 'mock fs.readFile return error'); | ||
fs.readFile('/etc/hosts', 'utf8', function (err, content) { | ||
// err.name === 'MockError' | ||
// err.message === 'mock fs.readFile return error' | ||
console.log(err); | ||
mm.restore(); // remove all mock effects. | ||
fs.readFile('/etc/hosts', 'utf8', function (err, content) { | ||
console.log(err); // => null | ||
console.log(content); // => your hosts | ||
}); | ||
}); | ||
``` | ||
### .data(module, propertyName, secondCallbackArg) | ||
```js | ||
mm.data(fs, 'readFile', new Buffer('some content')); | ||
// equals | ||
fs.readFile = function (args..., callback) { | ||
callback(null, new Buffer('some content')) | ||
}; | ||
``` | ||
### .empty(module, propertyName) | ||
```js | ||
mm.empty(mysql, 'query'); | ||
// equals | ||
mysql.query = function (args..., callback) { | ||
callback(); | ||
} | ||
``` | ||
### .datas(module, propertyName, argsArray) | ||
```js | ||
mm.datas(urllib, 'request', [new Buffer('data'), {headers: { foo: 'bar' }}]); | ||
// equals | ||
urllib.request = function (args..., callback) { | ||
callback(null, new Buffer('data'), {headers: { foo: 'bar' }}); | ||
} | ||
``` | ||
### .restore() | ||
```js | ||
// restore all mock properties | ||
mm.restore(); | ||
``` | ||
### .http.request(mockUrl, mockResData, mockResHeaders) and .https.request(mockUrl, mockResData, mockResHeaders) | ||
```js | ||
var mm = require('mm'); | ||
var http = require('http'); | ||
@@ -62,3 +181,3 @@ | ||
### Mock `http(s).request()` error | ||
### .http.requestError(mockUrl, reqError, resError) | ||
@@ -87,91 +206,30 @@ ```js | ||
### Mock async function callback error | ||
```js | ||
var mm = require('mm'); | ||
var fs = require('fs'); | ||
mm.error(fs, 'readFile', 'mock fs.readFile return error'); | ||
fs.readFile('/etc/hosts', 'utf8', function (err, content) { | ||
console.log(err); // should return mock err: err.name === 'MockError' | ||
mm.restore(); // remove all mock effects. | ||
fs.readFile('/etc/hosts', 'utf8', function (err, content) { | ||
console.log(err); // should return null | ||
console.log(content); // should show the host list | ||
}); | ||
}); | ||
``` | ||
### Mock `callback(null, data)` | ||
```js | ||
mm.data(fs, 'readFile', new Buffer('some content')); | ||
``` | ||
### Mock `callback(null, null)` | ||
```js | ||
mm.empty(mysql, 'query'); | ||
``` | ||
### Mock `callback(null, [data1, data2])` | ||
```js | ||
mm.datas(urllib, 'request', [new Buffer('data'), { headers: { foo: 'bar' } }]); | ||
``` | ||
### use `mm` just like [`muk`](https://github.com/fent/node-muk) | ||
```js | ||
var fs = require('fs'); | ||
var mm = require('mm'); | ||
mm(fs, 'readFile', function (path, callback) { | ||
process.nextTick(callback.bind(null, null, 'file contents here')); | ||
}); | ||
``` | ||
## Authors | ||
```bash | ||
$ git summary | ||
* fengmk2 <https://github.com/fengmk2> | ||
* dead-horse <https://github.com/dead-horse> | ||
* alsotang <https://github.com/alsotang> | ||
project : mm | ||
repo age : 1 year, 2 months | ||
active : 23 days | ||
commits : 55 | ||
files : 16 | ||
authors : | ||
49 fengmk2 89.1% | ||
4 dead-horse 7.3% | ||
1 AlsoTang 1.8% | ||
1 Alsotang 1.8% | ||
``` | ||
## License | ||
(The MIT License) | ||
This software is licensed under the MIT License. | ||
Copyright (c) 2012 - 2014 fengmk2 <fengmk2@gmail.com> | ||
Copyright (C) 2012 - 2014 fengmk2 <fengmk2@gmail.com> | ||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
27659
412
0
233
5
15
+ Addedco-sleep@~0.0.1
+ Addedenable@~1.0.0
+ Addedis-type-of@~0.3.1
+ Addedco-sleep@0.0.1(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedenable@1.0.2(transitive)
+ Addedis-type-of@0.3.1(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedmuk@0.3.2(transitive)
+ Addedsemver@4.1.1(transitive)
- Removedthunkify@0.0.1
- Removedmuk@0.3.1(transitive)
- Removedsemver@2.2.1(transitive)
- Removedthunkify@0.0.1(transitive)
Updatedmuk@~0.3.1
Updatedsemver@~4.1.0