spy, stub library for generators running on co.
Foggy require Generator Feature enabled environment, which could run co
library. Node(>=0.11.13) and iojs are ok for foggy. So please remember enable harmony features.
Install
$ npm install foggy
var foggy = require('foggy');
var co = require('co');
var context = {
generator: function* () {
return yeild Promise.resolve(true);
}
};
var spy = foggy.spy(context, 'generator');
co(context.generator).then(function (value) {
console.log(value); // true
console.log(spy.called); // true
});
Create a spy by foggy.spy()
method:
Create a anonymous spy.
var spy = foggy.spy();
Simply pass the generator in first argument.
var spy = foggy.spy(generator);
Create a proxy generator to replace object.generator
with same return value. spy.reset()
method could replace the original generator back.
var object = {
generator: function* () {
/* yields and return */
}
};
var spy = foggy.spy(object, 'generator');
One spy that contains properties / methods listed as below:
The number of recorded calls.
return true if the spy was called at least once.
return true if the spy was called exactly once.
return true if the spy was called exactly twice.
return true if the spy was called exactly thrice.
return the nth call object, which combines the informations of that generator call.
var gen = function* (obj) { return obj; };
var spy = foggy.spy(gen);
co.wrap(spy)({ test: 1 }).then(function () {
console.log(spy.getCall(0) === spy.firstCall); // true
console.log(spy.getCall(0).calledWith({ test: 1 })); // true
});
return the first call.
return the second call
return the third call
TODO
one call object's properties / methods:
Returns true if the call was called with the provided arguments.
var gen = function* (obj) { return obj; };
var spy = foggy.spy(gen);
var arg0 = {}, arg1 = [];
co.wrap(spy)(arg0, arg1).then(function () {
console.log(spy.firstCall.calledWith(arg0, arg1)); // true
console.log(spy.firstCall.calledWith(arg1, arg0)); // false
});
Returns the this
value for this generator call.
var context = { gen: function* (obj) { return obj; } };
var spy = foggy.spy(context, 'gen');
co(spy).then(function () {
console.log(spy.firstCall.thisValue === context); // true
});
returns the "return value" for the generator's result.
var gen = function* (a, b) { return a + b; };
var spy = foggy.spy(gen);
co.wrap(spy)(1, 2).then(function () {
console.log(spy.firstCall.returnValue); // 3
});
var foggy = require('foggy');
var co = require('co');
var context = {
generator: function* () {
return yield Promise.resolve(true);
}
};
var stub = foggy.stub(context, 'generator').returns(1);
co(context.generator).then(function(val) {
console.log(val); // 1
});
Create a stub by foggy.stub()
method:
Create an anonymous stub.
var stub = foggy.stub();
Create a stub generator to replace object.generator
. stub.reset()
method could replace the original generator back.
var object = {
generator: function* () {
/* yields and return */
}
};
var stub = foggy.stub(object, 'generator');
One stub generator contains properties / methods listed as below:
make stub return a provided value
which could be any object. If empty, it would return undefined
by default.
var stub = foggy.stub().returns(100);
co(stub).then(console.log); // 100
make stub throw a provided err
which could be any object. If empty, it would throws an Error
object.
var stub = foggy.stub().throws(100);
co(stub).catch(console.error); // 100
make stub return value from another generator.
var context = {
gen: function* () {
return 0;
}
};
var replacement = function* () {
return 1;
};
var stub = foggy.stub(context, 'gen').calls(replacement);
co(stub).then(console.log); // 1;
configure stub for a specific call.
var context = {
gen: function* () {
return 0;
}
};
var stub = foggy.stub(context, 'gen');
stub.onCall(0).returns(1);
stub.onCall(1).returns(2);
co(stub).then(console.log); // 1
co(stub).then(console.log); // 2
co(stub).then(console.log); // undefined
configure stub for first call.
configure stub for second call
configure stub for third call
configure stub for a specific arguments.
var stub = foggy.stub().returns(10);
stub.withArgs(1, 2).returns(100);
co.wrap(stub)(2, 1).then(console.log); // 10
co.wrap(stub)(1, 2).then(console.log); // 100
reset a stub which would replace the original generator back on the object.
var obj = {
generator: function* () {
return 0;
}
};
var stub = foggy.stub(obj, 'generator').returns(1);
co(obj.generator).then(function(val) {
console.log(val); // 1
stub.reset();
co(obj.generator).then(function(resetedReturnValue) {
console.log(resetedReturnValue); // 0
});
});
TODO
The MIT License (MIT)
Copyright (c) 2015 Wang Qiu
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 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.