make spy decorator
This commit is contained in:
parent
32fed2dd88
commit
f0888ac7bd
2 changed files with 26 additions and 4 deletions
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
Noiseless testing framework
|
||||
|
||||
Version: 1.0
|
||||
Version: 1.1
|
||||
License: MIT
|
||||
|
||||
## About
|
||||
|
|
@ -117,6 +117,29 @@ o.spec("call()", function() {
|
|||
})
|
||||
```
|
||||
|
||||
A spy can also wrap other functions, like a decorator:
|
||||
|
||||
```javascript
|
||||
//code to be tested
|
||||
var count = 0
|
||||
function inc() {
|
||||
count++
|
||||
}
|
||||
|
||||
//test suite
|
||||
var o = require("ospec")
|
||||
|
||||
o.spec("call()", function() {
|
||||
o("works", function() {
|
||||
var spy = o.spy(inc)
|
||||
spy()
|
||||
|
||||
o(count).equals(1)
|
||||
})
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
### Asynchronous tests
|
||||
|
||||
If a test body function declares a named argument, the test is assumed to be asynchronous, and the argument is a function that must be called exactly one time to signal that the test has completed. As a matter of convention, this argument is typically named `done`.
|
||||
|
|
@ -336,7 +359,7 @@ Declares that only a single test should be run, instead of all of them
|
|||
|
||||
---
|
||||
|
||||
### Function o.spy()
|
||||
### Function o.spy([Function fn])
|
||||
|
||||
Returns a function that records the number of times it gets called, and its arguments
|
||||
|
||||
|
|
|
|||
|
|
@ -25,8 +25,7 @@ module.exports = new function init() {
|
|||
spy.args = [].slice.call(arguments)
|
||||
spy.callCount++
|
||||
|
||||
if(fn)
|
||||
return fn.apply(this, arguments)
|
||||
if (fn) return fn.apply(this, arguments)
|
||||
}
|
||||
spy.args = []
|
||||
spy.callCount = 0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue