Skip to content

Instantly share code, notes, and snippets.

@limianwang
Last active December 2, 2016 00:16
Show Gist options
  • Save limianwang/1114249de99c6a189384 to your computer and use it in GitHub Desktop.
Save limianwang/1114249de99c6a189384 to your computer and use it in GitHub Desktop.
utility class
var requests = require('./requests.js');
function doSomethingWithResponse(resp, done) {
done(null, resp);
}
var requestObject = {};
var utilityClass = {
methodCreatesObject: function (callback) {
// Here’s the method I’m trying to stub:
requests.makeCallToAPI(requestObject, function (err, responseFromAPI) {
doSomethingWithResponse(responseFromAPI, function (err, finalObject) {
if (err) {
callback(err, null);
} else {
callback(null, finalObject); // <- Want to test the value of finalObject
}
});
});
}
}
module.exports = utilityClass;
$ ./node_modules/mocha/bin/mocha -R spec -u bdd test_index.js
test
✓ should be able to get object back
1 passing (9ms)
'use strict';
module.exports = {
makeCallToAPI: function(done) {
console.log('in original makeCallToAPI');
setImmediate(function() {
done(null, { a: 'z'});
});
}
};
var chai = require('chai');
var expect = chai.expect;
var sinon = require('sinon');
var requests = require('./requests');
var utilityClass = require('./');
describe('test', function() {
it('should be able to get object back', function(done) {
sinon.stub(requests, 'makeCallToAPI').yields(null, { c: 'd'});
utilityClass.methodCreatesObject(function(err, resp) {
expect(err).to.not.be.ok;
expect(resp).to.deep.equal({ c: 'd'});
requests.makeCallToAPI.restore();
done();
});
});
});
@neoadventist
Copy link

Did it work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment