Giter VIP home page Giter VIP logo

Comments (9)

thlorenz avatar thlorenz commented on July 29, 2024

Change db to nano in your test since that is what you are requiring. Rereading the read me may help as well since all this us explained in there with lots of examples

from proxyquire.

ghachey avatar ghachey commented on July 29, 2024

Hi,

Thanks for replying. Yes, this is what I was trying before and still could not make it work. I did play around with examples and got them all to work. But it seems all your examples make use of a 'require' that all return an 'object' (e.g. your samples foo, bar, stats and node's fs and path all return an object). The nano module returns a 'function' when doing require('nano'). I don't know if this has anything to do with it, but this is what I get when trying to execute my tests.

TypeError: object is not a function
      at Object.<anonymous> (/Users/ghachey/Development/website/app/models/couchdb.js:11:10)
      at Module._compile (module.js:456:26)
      at Object.Module._extensions..js (module.js:474:10)
      at Object.require.extensions.(anonymous function) (/Users/ghachey/Development/website/node_modules/proxyquire/lib/proxyquire.js:235:43)
      at Module.load (module.js:356:32)
      at Function.Module._load (module.js:312:12)
      at Module.require (module.js:364:17)
      at Proxyquire._withoutCache (/Users/ghachey/Development/website/node_modules/proxyquire/lib/proxyquire.js:169:12)
      at Proxyquire.load (/Users/ghachey/Development/website/node_modules/proxyquire/lib/proxyquire.js:131:15)
      at Context.<anonymous> (/Users/ghachey/Development/website/test/specs/models/couchdb.js:25:15)

Looking at your lib/proxyquire.js it seems it only works on objects. Sorry if I'm still missing something obvious.

Thanks

from proxyquire.

thlorenz avatar thlorenz commented on July 29, 2024

Should work with functions too, try to take sinon out of the picture for a second -- looks like you are passing an object where a function is expected, i.e. try:

couchdb = proxyquire(
  '../../../app/models/couchdb', 
  { db: { get: function () { console.log('getting struff' ) } } 
});

from proxyquire.

ghachey avatar ghachey commented on July 29, 2024

I tried to take out sinon as suggested and can't get it to work properly though I did partially get it to work with a slightly different approach. I'll try to provide details on what did not work and what did work.

Not working
Trying to assign var nano = require('nano')('http://localhost:5984/mydb'); I can't get it working and get the above TypeError: object is not a function for everything I tried. I've tried various things along the line of what follows to no avail. Note I leave several lines commented so you can see various permutations I tried.

Code:

var nano = require('nano')('http://localhost:5984/mydb');

module.exports.getDocs = function(docs, callback){
  // some logic
  nano.get(doc, function(err, body) {
    callback(err, body);
  });
};

Test:

var proxyquire = require('proxyquire');
var nano = require('nano')('http://localhost:5984/mydb');
var couchdb; // import using proxy below

describe('CouchDB document model', function(){
  var nanoStub = {};
  nanoStub.get = function (url, callback) { 
    console.log('doing stuff: ', url);
    callback(null, {'some': 'data'}); 
  };
  // nanoStub.get = function () { console.log('stuff'); };
  // nanoStub.get = function () { return 'something'; };
  // nanoStub.get = {'some': 'data'};

  before(function () {
    couchdb = proxyquire('../../../app/models/couchdb', { nano: {get: nanoStub.get} });
    // couchdb = proxyquire('../../../app/models/couchdb', { nano: {get: nanoStub} });
    // couchdb = proxyquire('../../../app/models/couchdb', { nano: nanoStub });
    // couchdb = proxyquire('../../../app/models/couchdb', { nano: nanoStub.get });
  });

  describe('getDocs function', function(){
    it('should use fake couchdb data', function(done) {
      couchdb.getDocs(docs, function(err, body) {
        // Never gets here, Type error thrown as previous post
        done();
      });
    });
  });

});

Partially Working
If I stub the whole of nano as follows then get.db executes the fake function when calling getDocs. However, I no longer have access to all non-overridden method as the whole thing is stubbed. This does not seem to be inline with all the examples provided. So I'm assuming I am not using this correctly.

Code:

var nano = require('nano')
var db = nano('http://localhost:5984/mydb');

module.exports.getDocs = function(docs, callback){
  // some logic
  db.get(doc, function(err, body) {
    callback(err, body);
  });
};

Test:

var proxyquire = require('proxyquire');
var nano = require('nano');
var db = nano('http://localhost:5984/mydb');
var couchdb; // import using proxy below

describe('CouchDB document model', function(){
  var nanoStub = function () { 
    return {
      get: function() {
        console.log('do stuff');
      }
    }; 
  };

  before(function () {
    couchdb = proxyquire('../../../app/models/couchdb', { nano: nanoStub });
  });

  describe('getDocs function', function(){
    it('should use fake couchdb data', function(done) {
      couchdb.getDocs(docs, function(err, body) {
        // Getting body with data stubbed data
        done();
      });
    });
  });

});

A bit more nano specific details which might help clarify. Requiring the module nano returns the following.

> var nano = require('nano');
var nano = require('nano');
undefined
> nano
nano
{ [Function: database_module]
  version: '5.10.0',
  path: '/Users/ghachey/Development/node_modules/nano' }
> typeof nano
typeof nano
'function'

But then the functions of interest (the ones I want to stub for tests) are only accessible after executing the function like so.

> var db = nano('http://localhost:5984/mydb');
> db
{ info: [Function],
  ...
  get: [Function: get_doc]
  ,... }
> typeof db
'object'

I have a feeling I'm pretty close but...

from proxyquire.

justrhysism avatar justrhysism commented on July 29, 2024

I'm having the exact same issue. Have you had any luck with this one?

from proxyquire.

ghachey avatar ghachey commented on July 29, 2024

No. I needed to move on and abandonned for now.

from proxyquire.

justrhysism avatar justrhysism commented on July 29, 2024

Damn! Cheers for the prompt response.

from proxyquire.

bendrucker avatar bendrucker commented on July 29, 2024

Closing this as it's not clear what's going on here

from proxyquire.

chrissharp80 avatar chrissharp80 commented on July 29, 2024

@bendrucker it's very clear what's going on. He was trying to stub the get function of the nano library. And no one helped. Now I'm facing the same problem, 5 years later, and I find this issue, with my exact problem closed with no answer besides "read the docs" and "it's not clear". It would be great to actually get someone to try to stub out a function in nano and see what happens. Thanks.

from proxyquire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.