Sunday, 12 March 2017

google-geocoder with proxyquire and sinon

I'm still very much learning node, js, sinon, proxyquire, etc.

I have a module that uses the google-geocode module (https://github.com/bigmountainideas/google-geocoder) and I am struggling to write a test to stub it.

This all boils down I think to how you set it up. In time.js I do as follows as per google-geocoder documentation:

var geocoder = require('google-geocoder');

  ...

module.exports = function(args, callback) {
  var geo = geocoder({ key: some-thing });
  geo.find('new york', function(err, response) { ... });
}

I'm trying to test as follows but I get the error:

  TypeError: geo.find is not a function
   at run (cmdsUser/time.js:x:x)
   at Context.<anonymous> (tests/cmdsUser/time-test.js:x:x)

time-test.js:

var time;
var findStub;

before(function () {
  findStub = sinon.stub()
  time = proxyquire('./../../cmdsUser/time',{ 'google-geocoder': { find: findStub } } );
});

describe('Demo test', function() {
  it('Test 1', function(done){
    findStub.withArgs('gobbledegook').yields(null, { this-is: { an-example: 'invalid' } });

    time(['gobbledegook'], function(err, response) {
      expect(response).to.equals('No result for gobbledegook');
      done();
    });
  });
});

I am a little confused. Many thanks.



via s27840

No comments:

Post a Comment