Last active
December 2, 2016 00:16
-
-
Save limianwang/1114249de99c6a189384 to your computer and use it in GitHub Desktop.
utility class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./node_modules/mocha/bin/mocha -R spec -u bdd test_index.js | |
test | |
✓ should be able to get object back | |
1 passing (9ms) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
module.exports = { | |
makeCallToAPI: function(done) { | |
console.log('in original makeCallToAPI'); | |
setImmediate(function() { | |
done(null, { a: 'z'}); | |
}); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did it work?