I've got the following style of function (using bluebird promises under Node.JS):
module.exports = {
somefunc: Promise.method(function somefunc(v) {
if (v.data === undefined)
throw new Error("Expecting data");
v.process_data = "xxx";
return module.exports.someother1(v)
.then(module.exports.someother2)
.then(module.exports.someother3)
.then(module.exports.someother4)
.then(module.exports.someother5)
.then(module.exports.someother6);
}),
});
which I'm trying to test (using mocha, sinon, assert):
// our test subject
something = require('../lib/something');
describe('lib: something', function() {
describe('somefunc', function() {
it("should return a Error when called without data", function(done) {
goterror = false;
otherexception = false;
something.somefunc({})
.catch(function(expectedexception) {
try {
assert.equal(expectedexception.message, 'Expecting data');
} catch (unexpectedexception) {
otherexception = unexpectedexception;
}
goterror = true;
})
.finally(function(){
if (otherexception)
throw otherexception;
assert(goterror);
done();
});
});
});
All of which works as such, but feels convoluted for one.
My MAIN problem is testing the Promises-chain (and order) in the function. I've tried several things (faking an object with a then method, which didn't work; mocking it like crazy, etc); but there seems to be something I'm not seeing, and I don't seem to be getting the mocha- or sinon- documentations in this regard.
Anybody got any pointers?
Thanks
count
Mocha supports promises so you could do this
describe('lib: something', function() {
describe('somefunc', function() {
it("should return a Error when called without data", function() {
return something.somefunc({})
.then(assert.fail)
.catch(function(e) {
assert.equal(e.message, "Expecting data");
});
});
});
});
This is roughly equivalent to the synchronous code:
try {
something.somefunc({});
assert.fail();
} catch (e) {
assert.equal(e.message, "Expecting data");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.