javascript - Is attaching dependencies to a class an appropriate way to test? -



javascript - Is attaching dependencies to a class an appropriate way to test? -

i'm struggling bit moving on tdd/bdd. have multiple methods rely on response external module in order perform responsibility. test these methods under different circumstances, instance if module responds info object or error.

one way of doing attach external module class/module beingness tested properties , "stubbing" it. seems little dirty though, because end these other modules attached module.

is appropriate way testing methods rely on response external module, or there improve way?

should restructuring architecture in way create more tested? if how?

here's illustration of mean:

my-module.js

var extmod = require('external-module'); var mymod = module.exports = function () { /* stuff */ }; mymod.prototype.extmod = extmod; mymod.prototype.somemethod = function () { this.extmod({ /* request */ }, function () { /* stuff */ }); };

mymodulespec.js

i using mocha, chai, , sinon in example

var chai = require('chai'); var expect = chai.expect; var sinon = require('sinon'); var sinonchai = require('sinon-chai'); var mymod = require('../lib/my-module.js'); chai.use(sinonchai); describe('my module', function () { /* tests */ describe('.somemethod()', function () { it('should when info object returned', function () { var foo = new mymod(); var stub = sinon.stub(foo, 'extmod').returns({ id: 1, name: 'some name' }); var result = foo.somemethod(); expect(result).to.be.true; }); it('should when error returned', function () { var foo = new mymod(); var err = new error('something terrible happened.'); var stub = sinon.stub(foo, 'extmod').returns(err); var result = foo.somemethod(); expect(result).to.be.false; }); }); }); alternative

i considered creating method returns whatever pass it, running external methods through allow stubbing without attaching external modules class. issue here stub 1 external method, doable.

like this:

var extmod = require('external-module'); var mymod = module.exports = function () { /* stuff */ }; mymod.prototype.extmod = function (module) { homecoming module; }; mymod.prototype.somemethod = function () { var extmodule = this.extmod(extmod); extmodule({ /* request */ }, function () { /* stuff */ }); };

javascript node.js tdd bdd

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -