{"_id":"spy","_rev":"19-f9ce0a22ccca4ae836387238a748c1bd","name":"spy","time":{"modified":"2022-06-26T23:12:12.828Z","created":"2011-07-22T18:09:14.677Z","0.0.0":"2011-07-22T18:09:14.755Z","0.1.0-alpha":"2014-05-24T03:06:32.397Z","0.1.0":"2014-08-11T05:57:07.595Z","0.1.1":"2014-08-11T15:26:31.346Z","0.1.2":"2014-08-20T15:32:05.694Z","0.1.3":"2014-08-21T03:32:30.115Z","1.0.0":"2016-12-06T06:53:39.800Z"},"maintainers":[{"name":"popomore","email":"sakura9515@gmail.com"}],"dist-tags":{"latest":"1.0.0"},"description":"spy and mock for simple testcase","readme":"# spy\n\n[![NPM version](https://img.shields.io/npm/v/spy.svg?style=flat)](https://npmjs.org/package/spy)\n[![Spm version](http://spmjs.io/badge/spy)](http://spmjs.io/package/spy)\n[![Build Status](https://img.shields.io/travis/popomore/spy.svg?style=flat)](https://travis-ci.org/popomore/spy)\n[![Build Status](https://img.shields.io/coveralls/popomore/spy.svg?style=flat)](https://coveralls.io/r/popomore/spy)\n[![NPM downloads](http://img.shields.io/npm/dm/spy.svg?style=flat)](https://npmjs.org/package/spy)\n\nspy and mock for simple testcase\n\n---\n\n## Why Spy?\n\nSinon is the best spy, stub, mock lib, I use it in my every repo.\n\nBut Sinon is too big, I just use spy and simple mock, and I think it can be separated to different repo.\n\nSo I write Spy\n\n- written in commonjs, support package mananger for browser such as spm, component, bower.\n- support spy and simple mock, see API below\n\n## Install\n\n```\n$ npm install spy -g\n```\n\nInstall for browser\n\n[spm](http://spmjs.io/)\n\n```\nspm install spy\n```\n\n[component](http://component.io/)\n\n```\ncomponent install popomore/spy\n```\n\n[bower](http://bower.io/)\n\n```\nbower install spy\n```\n\n## Usage\n\n```\nvar spy = require('spy');\nvar s = spy();\ns(1);\ns.called // return true\ns.callCount // return 1\ns.calledWith(1) // return true\n```\n\nmock spy function\n\n```\nvar spy = require('spy');\nvar s = spy();\ns.mock(1);\ns(); // return 1\n```\n\nmock module\n\n```\n// a.js\nmodule.exports = function() {}\n\n// b.js\nvar a = require('spy').require('./a.js');\na();\na.callCount // return 1\n```\n\n## API\n\n### spy()\n\nCreate a anonymous `Spy` function\n\n### spy(func)\n\nCreate a `Spy` function wrapped `func`\n\n### spy(obj, 'func')\n\nCreate a `Spy` function wrapped `obj.func`\n\n### Spy\n\n`Spy` function can be used as a normal function, and it will record what the function do.\n\nBy default, Spy will pass arguments to original function, get result from the function, and use the same context;\n\nOtherwise, using `spy.mock` will call the mock function instead of calling the original function, \n\n#### spy.obj\n\nThe object that contain the refer to the function\n\n```\nspy(obj, 'a'); // obj === spy.obj\n``` \n\n#### spy.method\n\nThe original method\n\n```\nspy(obj, 'a'); // obj.a === spy.method\nspy(func); // func === spy.method\nspy(); // spy.method is an anonymous function\n```\n\n#### spy.methodName\n\nThe method name of the given function\n\n```\nspy(obj, 'a'); // 'a' === spy.method\n``` \n\n#### spy.called\n\nReturn true if the spy function has been called.\n\n#### spy.callCount\n\nReturn the count that the spy function has been called.\n\n#### spy.calls\n\nContain the call list in order, each call is the `Call` instance.\n\n```\nspy(); // spy.call[0]\nspy(); // spy.call[1]\n```\n\n#### spy.mock()\n\nMock the return value of the spy\n\n```\nspy.mock(1);\nspy(); // return 1\n```\n\nYou can do it just like a normal function\n\n```\nspy.mock(function(arg1, arg2, arg3) {\n\t// 1. return arg1\n\t// 2. return this\n\t// 3. throw new Error();\n});\n```\n\nSupport generator!!!\n\n```\nspy.mock(function* () {\n  var pkg = yield read('package.json');\n  return pkg.name;\n});\n```\n\n#### spy.reset()\n\nReset the call record and the mock method\n\n#### spy.restore()\n\nRestore the refer to the function\n\n#### spy.calledBefore / spy.calledAfter\n\nIn the call sequence, determine whether one spy is called before/after the other.\n\n```\nspy1();\nspy2();\nspy1.calledBefore(spy2); // true\nspy2.calledAfter(spy1); // true\n```\n\n#### spy.calledWith / spy.alwaysCalledWith / spy.neverCalledWith\n\nSame as call.calledWith, but will match spy.calls in diffent ways.\n\n- `spy.calledWith`, whether one of the calls called with arguments, just like\n\n  ```\n  calls.some(function(call){\n    return call.calledWith()\n  });\n  ```\n\n- `spy.alwaysCalledWith`, whether all calls called with arguments, just like\n\n  ```\n  calls.every(function(call){\n    return call.calledWith()\n  });\n  ```\n\n- `spy.neverCalledWith`, whether all calls didn't call with arguments, just like\n\n  ```\n  calls.every(function(call){\n    return !call.calledWith()\n  });\n  ```\n\n\n#### spy.calledWithNew / spy.alwaysCalledWithNew / spy.neverCalledWithNew\n\nSame as spy.calledWithNew, but will match spy.calls in diffent ways.\n\nThe different between always and never see spy.calledWith\n\n#### spy.calledWithExactly / spy.alwaysCalledWithExactly / spy.neverCalledWithExactly\n\nSame as spy.calledWithExactly, but will match spy.calls in diffent ways.\n\nThe different between always and never see spy.calledWith\n\n#### spy.calledOn / spy.alwaysCalledOn / spy.neverCalledOn\n\nSame as spy.calledOn, but will match spy.calls in diffent ways.\n\nThe different between always and never see spy.calledWith\n\n#### spy.returned / spy.alwaysReturned / spy.neverReturned\n\nSame as spy.returned, but will match spy.calls in diffent ways.\n\nThe different between always and never see spy.calledWith\n\n#### spy.threw / spy.alwaysThrew / spy.neverThrew\n\nSame as spy.threw, but will match spy.calls in diffent ways.\n\nThe different between always and never see spy.calledWith\n\n### Call\n\n`Call` object will record every function call.\n\n#### call.calledWith\n\nReturn true if match the call arguments\n\n```\nspy(1, 2, 3)\nspy.calls[0].calldWith(1) // true\nspy.calls[0].calldWith(1, 2) // true\nspy.calls[0].calldWith(1, 2, 3) // true\n```\n\n#### call.calledWithExactly\n\nSame as calledWith, but assert the arguments length\n\n```\nspy(1, 2, 3)\nspy.calls[0].calledWith(1) // false\nspy.calls[0].calledWith(1, 2) // false\nspy.calls[0].calledWith(1, 2, 3) // true\n```\n\n#### call.calledWithNew\n\nReturn true if it instantiation\n\n```\nnew spy()\nspy.calls[0].calledWithNew() // true\n```\n\n#### call.calledOn\n\nReturn true if it called with the context\n\n```\nspy.call(ctx)\nspy.calls[0].calledOn(ctx) // true\n```\n\n#### call.returned\n\nReturn true if get the matched return\n\n```\nfunction spiedFunc() { return 1; }\nvar spy = require('spy')(spiedFunc);\nspy()\nspy.calls[0].returned(1) // true\n```\n\n#### call.threw\n\nReturn true if the function had threw\n\n```\nfunction spiedFunc() { throw new Error(); }\nvar spy = require('spy')(spiedFunc);\nspy()\nspy.calls[0].threw() // true\n```\n\n## LICENSE\n\nCopyright (c) 2014 popomore. Licensed under the MIT license.\n","versions":{"0.1.0-alpha":{"name":"spy","version":"0.1.0-alpha","description":"spy implement for test","main":"index","dependencies":{},"devDependencies":{"istanbul":"~0.2.7","coveralls":"~2.10.0","mocha":"~1.18.2","should":"~3.3.1"},"repository":{"type":"git","url":"https://github.com/popomore/spy"},"homepage":"https://github.com/popomore/spy","author":{"name":"popomore","email":"sakura9515@gmail.com"},"license":"MIT","scripts":{"test":"make test"},"bugs":{"url":"https://github.com/popomore/spy/issues"},"_id":"spy@0.1.0-alpha","dist":{"shasum":"33a89413677fbca85529b2d7cd396108672d3ca6","tarball":"https://registry.npmjs.org/spy/-/spy-0.1.0-alpha.tgz","integrity":"sha512-VEW4nKqj/CHf6yDuWLNpFp2gAono/APQb8WpDBnDOWYi2GP2w/FiSJlv2S5VYldagbc7IVWKHgcaJZd7bSyO7w==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIHlpiD1mADuTXMFEk5AnD3BuwohZGoPq2F4PFGZ70xupAiBv8rx+Av+DKm3OdMTRinmDovvH4OWZnW77Em8CvrjLeQ=="}]},"_from":".","_npmVersion":"1.4.6","_npmUser":{"name":"popomore","email":"sakura9515@gmail.com"},"maintainers":[{"name":"popomore","email":"sakura9515@gmail.com"}]},"0.1.0":{"name":"spy","version":"0.1.0","description":"spy and mock for simple testcase","main":"index","dependencies":{},"devDependencies":{"istanbul":"~0.2.7","coveralls":"~2.10.0","mocha":"~1.18.2","should":"~3.3.1"},"repository":{"type":"git","url":"https://github.com/popomore/spy"},"homepage":"https://github.com/popomore/spy","author":{"name":"popomore","email":"sakura9515@gmail.com"},"license":"MIT","scripts":{"test":"make test"},"spm":{"main":"lib/spy.js"},"bugs":{"url":"https://github.com/popomore/spy/issues"},"_id":"spy@0.1.0","dist":{"shasum":"f2e2f1bab0648a671dbf76738373a2ce36322a31","tarball":"https://registry.npmjs.org/spy/-/spy-0.1.0.tgz","integrity":"sha512-HNQwM1y10D+xoN12/0HgGMljar+0dXXOw2ODd2nX/xKHJ0fT+qgYN2M/EJCyVbQS8xtVGZzPhfHS3lLCphbMXg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEvdWEGUhxwN35SKmyTtUIbgPzV+BtRHevYMGI/Zy3u0AiBqZTC6VTqrsUJGIupY6D2G8yl618CJSgqVrRhQ0W93ag=="}]},"_from":".","_npmVersion":"1.4.6","_npmUser":{"name":"popomore","email":"sakura9515@gmail.com"},"maintainers":[{"name":"popomore","email":"sakura9515@gmail.com"}]},"0.1.1":{"name":"spy","version":"0.1.1","description":"spy and mock for simple testcase","main":"index","dependencies":{},"devDependencies":{"istanbul":"~0.2.7","coveralls":"~2.10.0","mocha":"~1.18.2","should":"~3.3.1"},"repository":{"type":"git","url":"https://github.com/popomore/spy"},"homepage":"https://github.com/popomore/spy","author":{"name":"popomore","email":"sakura9515@gmail.com"},"license":"MIT","scripts":{"test":"make test"},"spm":{"main":"lib/spy.js"},"bugs":{"url":"https://github.com/popomore/spy/issues"},"_id":"spy@0.1.1","dist":{"shasum":"b5400994ca42ee769ed83a1acc404f9c17d0ec54","tarball":"https://registry.npmjs.org/spy/-/spy-0.1.1.tgz","integrity":"sha512-dN4/oFe40EoCiLdftMLZSHeghZl0WIPqhqmG2uCwAxnZt1QD7u50FTrK21etJ9tHN0vgiaf3JSxFGsPw8nL6XQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDx0/ygXPXohNFq+N8a4HJ0cE/LrfFHD+Q5MR0Ybw+0bgIhAP54FtNQX/1EuW3H+CRGOH6XoHK8iTCuMpKDk7oc4uZq"}]},"_from":".","_npmVersion":"1.4.3","_npmUser":{"name":"popomore","email":"sakura9515@gmail.com"},"maintainers":[{"name":"popomore","email":"sakura9515@gmail.com"}]},"0.1.2":{"name":"spy","version":"0.1.2","description":"spy and mock for simple testcase","main":"index","dependencies":{},"devDependencies":{"istanbul":"~0.2.7","coveralls":"~2.10.0","mocha":"~1.18.2","should":"~3.3.1"},"repository":{"type":"git","url":"https://github.com/popomore/spy"},"homepage":"https://github.com/popomore/spy","author":{"name":"popomore","email":"sakura9515@gmail.com"},"license":"MIT","scripts":{"test":"make test"},"spm":{"main":"lib/spy.js"},"testling":{"harness":"mocha-bdd","files":"test/*.test.js","browsers":["ie/6..latest","chrome/latest","firefox/latest","safari/latest","opera/11.0..latest","iphone/6","ipad/6","android-browser/latest"]},"bugs":{"url":"https://github.com/popomore/spy/issues"},"_id":"spy@0.1.2","dist":{"shasum":"0fd46655d89767a061a1c9b10f773280a42433d0","tarball":"https://registry.npmjs.org/spy/-/spy-0.1.2.tgz","integrity":"sha512-uv4UGmX1rRDSjQBiCE8KMfZGUVEfzvPE75U1Zo66P0lG1t9B6oOZYSk1tu4XBfdifMxL3N85SyFYD6fMop6r+w==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC3BPmqI+8rFJiRlhVNQLLZU8EU6nW+xgsMOEMBqSTO8QIgfAWCPnLRpeCBiA7bMYiFlCDN2UmRFiWCmKyvatybzNQ="}]},"_from":".","_npmVersion":"1.4.3","_npmUser":{"name":"popomore","email":"sakura9515@gmail.com"},"maintainers":[{"name":"popomore","email":"sakura9515@gmail.com"}]},"0.1.3":{"name":"spy","version":"0.1.3","description":"spy and mock for simple testcase","main":"index","dependencies":{},"devDependencies":{"istanbul":"~0.2.7","coveralls":"~2.10.0","mocha":"~1.18.2","should":"~3.3.1"},"repository":{"type":"git","url":"https://github.com/popomore/spy"},"homepage":"https://github.com/popomore/spy","author":{"name":"popomore","email":"sakura9515@gmail.com"},"license":"MIT","scripts":{"test":"make test"},"spm":{"main":"lib/spy.js"},"testling":{"harness":"mocha-bdd","files":"test/*.test.js","browsers":["ie/6..latest","chrome/latest","firefox/latest","safari/latest","opera/11.0..latest","iphone/6","ipad/6","android-browser/latest"]},"bugs":{"url":"https://github.com/popomore/spy/issues"},"_id":"spy@0.1.3","dist":{"shasum":"825c33968efa996a3c8bcfbfd7892b74df060a66","tarball":"https://registry.npmjs.org/spy/-/spy-0.1.3.tgz","integrity":"sha512-B5mCQ0XxnDPn/IlfFsE62aekJnyUBKyt05Bozw5iGNGQyKdbI5yATWi+by18ZTewWyoHd3wKm9XuYk3tu2iH6g==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDSbG7KC9ZJnXeaRxVuXXdgiXEnpiFxZNENK7kIpoUOuAIhAJO+jIt0UOvCDgZpv77AYvEYPiOhPha4PmM2+1P8yIcN"}]},"_from":".","_npmVersion":"1.4.3","_npmUser":{"name":"popomore","email":"sakura9515@gmail.com"},"maintainers":[{"name":"popomore","email":"sakura9515@gmail.com"}]},"1.0.0":{"name":"spy","version":"1.0.0","description":"spy and mock for simple testcase","dependencies":{},"devDependencies":{"egg-bin":"^1.7.0","egg-ci":"^1.1.0","eslint":"^3.11.1","eslint-config-egg":"^3.2.0","should":"~3.3.1"},"repository":{"type":"git","url":"git+https://github.com/popomore/spy.git"},"homepage":"https://github.com/popomore/spy","author":{"name":"popomore","email":"sakura9515@gmail.com"},"license":"MIT","scripts":{"lint":"eslint .","test":"npm run lint && egg-bin test","cov":"egg-bin cov","ci":"npm run lint && egg-bin cov"},"testling":{"harness":"mocha-bdd","files":"test/*.test.js","browsers":["ie/6..latest","chrome/latest","firefox/latest","safari/latest","opera/11.0..latest","iphone/6","ipad/6","android-browser/latest"]},"files":["index.js","lib"],"ci":{"version":"4, 6, 7"},"gitHead":"b9e6457b311abc8a7b46806c4bee4e8841123f72","bugs":{"url":"https://github.com/popomore/spy/issues"},"_id":"spy@1.0.0","_shasum":"2aaa22af1d8696b30d0596db1d12781c91acf6ad","_from":".","_npmVersion":"3.10.8","_nodeVersion":"6.9.1","_npmUser":{"name":"popomore","email":"sakura9515@gmail.com"},"dist":{"shasum":"2aaa22af1d8696b30d0596db1d12781c91acf6ad","tarball":"https://registry.npmjs.org/spy/-/spy-1.0.0.tgz","integrity":"sha512-UPZwZSOuEj1InzelgmBPj3f74qywS99VCJVklZVnhXEnZjwTLe+PybMxBXWWr6Aiu140cFLAEmMop5YsC++Jog==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEbHrc9PEtFS+KXpgvRWsvIWWcOpMb1zLqkqnlIpWVzRAiAtpxi49NTteRW+EQVTVBgiCr+je3nqg5GY4Vdy6YPx9Q=="}]},"maintainers":[{"name":"popomore","email":"sakura9515@gmail.com"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/spy-1.0.0.tgz_1481007217755_0.5623102537356317"}}},"homepage":"https://github.com/popomore/spy","repository":{"type":"git","url":"git+https://github.com/popomore/spy.git"},"author":{"name":"popomore","email":"sakura9515@gmail.com"},"bugs":{"url":"https://github.com/popomore/spy/issues"},"license":"MIT","readmeFilename":"README.md","users":{"mysticatea":true,"atian25":true}}