Comments (9)
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.
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.
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.
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.
I'm having the exact same issue. Have you had any luck with this one?
from proxyquire.
No. I needed to move on and abandonned for now.
from proxyquire.
Damn! Cheers for the prompt response.
from proxyquire.
Closing this as it's not clear what's going on here
from proxyquire.
@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)
- Problem when stubbing for just one test HOT 1
- Unable to stub indirect improts HOT 3
- TypeScript definitions HOT 1
- Using `@global` on path with subdir causes Node to throw MODULE_NOT_FOUND error HOT 4
- NodeJS karma proxyquire gives Error: ENOENT: no such file or directory, scandir C:\\<path to project>\\[object Object][object Object] HOT 1
- Examples of use with classes HOT 2
- Cannot proxy module sub-item HOT 3
- Mocks the first require only HOT 1
- Unable to resolve module `module` from `node_modules/proxyquire/lib/proxyquire.js` HOT 1
- Slightly confusing first example in readme HOT 1
- `createRequire` support & a way to access the mocked `require` HOT 6
- Stubbing a dependency within a base class HOT 2
- proxyquire jasmine expect not working - 1 test 0 assertions HOT 2
- How to use multiple ways to override the imported function in controller using proxyquire HOT 1
- Stubbing peer dependencies HOT 3
- Can we stub pg library of nodejs using proxyquire? HOT 1
- Proxyquire compatibility with ESM HOT 1
- Proxyquire not working with node@16? HOT 1
- Cannot read property 'require' of undefined when using ES6 modules HOT 5
- module exporting a function HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proxyquire.