I have something like:
sandbox.stub(rp, 'get').resolves(successResponse)
which returns my custom response when it hits this code:
return await rp.get(url, options)
But how can I do something like this:
sandbox.stub(rp).resolves(successResponse)
Which can return a custom response when it hits this code?
return await rp(url, options)
When I attempt "stubbing" the entire object, I get this error when I run the test:
TypeError: Attempted to wrap undefined property undefined as function
at wrapMethod (node_modules\sinon\lib\sinon\util\core\wrap-method.js:70:21)
at stub (node_modules\sinon\lib\sinon\stub.js:58:44)
at Object.stub (node_modules\sinon\lib\sinon\collection.js:93:33)
rp is request-promise-native, which wraps request
From #Troopers' link in the comments above, it seems like this isn't possible: Doing this is not technically possible without faking the entire module loading system.
So I followed the suggestion here: https://github.com/request/request/issues/2181 and used mock-require to stub rp. I also changed up my code that used to call rp.get(), so that it just calls rp(), since I couldn't also figure out how to stub both rp() and rp.get()
You might find the Sinon HowTo about Link Seams helpful: http://sinonjs.org/how-to/link-seams-commonjs/
Related
It seems that, when I use thenReturn like this:
when(abc.call()).thenReturn(a).thenReturn(b),
I expect:
verify(abc, times(2)).call()
instead, the method seems only get called once, I am a little confused(my test work as expected, the mock seems return the value I expected), but for the invocation times, I don't know if I am getting the wrong result, or it's a expected behavior of the Mockito?
when() is mocking the abc.call() method which has to produce a certain return type. If the method is called once, it will return the value also once, so the stubbing for method invocation is done only once hence the verify only recognizes one call.
You need to customize your function so that the stub(abc.call()) gets called more than once.
You can follow this thread for implementing multiple stubs
I have a controller that has a method that uses GET to return an object (json format). I want to call it from another controller.
In order to make it easier to understand, let's call the controllers controllerSrc (source) and controllerTgt (target).
First, I import the controller and instantiate the variable of the method.
const controllerSrc = require('controllerSrc')
const controllerSrcGET= controllerSrc.get
In theory, the controllerSrcGET returns a json object, so when I try to do:
console.log(controlerSrcGET._id)
I should have there the id of the object and work arount it. However, I get undefined.
Extra info
I am working with Morgan, and I've noticed that the GET request that should be printed is not. So I'm guessing that it is not enough to instantiate the controllerSrcGET(?)
I am going through some code on github: https://github.com/linnovate/mean/blob/master/server/routes/user.route.js
But there is a portion of it I don't understand, that is:
router.route('/')
.post(asyncHandler(insert));
On npm express-async-handler
is described as:
Simple middleware for handling exceptions inside of async express routes and passing them to your express error handlers.
They go give an example of how to use the module, but it doesn't explain much.
So my questions are:
How is the insert function on line 12 called without parentheses?
What is the function of asyncHandler(), what would the code look like if you decide on not using it?
Normally when using router.route('/').post there are curly braceswhich follow. In this code I cant see any. So my question are: Is the async function insert part of the function body of router.route('/').post? and if not then why are there no curly braces?
What exactly is being exported here user.controller.js on line 14 (is it an object, a var...)? What is the advantage of exporting it this way? Why not just export the function insert()?
Thank you in advance.
How is the insert function on line 12 called without parentheses?
The insert function is not called here. It is passed to asyncHandler() as a function reference so it can be called later. It is asyncHandler() which is called immediately and that returns a new function that is passed to .post() as the request handler.
What is the function of asyncHandler(), what would the code look like if you decide on not using it?
This is a wrapper around insert that looks for a rejected promise returned from the function and, if found, calls next(err) automatically.
Normally when using router.route('/').post there are curly braceswhich follow. In this code I cant see any. So my question are: Is the async function insert part of the function body of router.route('/').post? and if not then why are there no curly braces?
I'm not sure what you mean by curly braces. .post() expects a function reference to be passed to it that will be called with a certain set of parameters when the defined route matches an incoming request. It can use as either of these:
// externally defined request handler function
router.route('/').post(someRequestHandlerFunc);
// inline defined request handler function
router.route('/').post(function(req, res, next) {
// code here for the request handler
});
What exactly is being exported here user.controller.js on line 14 (is it an object, a var...)? What is the advantage of exporting it this way? Why not just export the function insert()?
I'm assuming the line 14, you're asking about is here. That's just exporting the insert function on the insert property of this modules exports. When you export a function, you don't use insert(). That calls the function immediately. You just refer to the function's name as insert to export a reference to the function that can be called later.
The reason to export is as a property of an object rather than just export the single function is to make the module extensible so it can export other things as different named properties.
I am trying to create a Node module (using harmony) that upon loading by another module/application, has to be yielded to so that things in it's construct can be executed and loaded before any of it's exposed functions can be called.
The issue I am having is that I cannot seem to yield to the internal function that is being executed, using module.exports. An example would help.
module.exports = function*(s_id){
console.log('loading the module lets it execute up till here');
if (!(this instanceof Tester)) return yield new Tester();
}
function* Tester(){
console.log('but we never execute this generator function');
}
Tester.prototype = {
model : function*(){
// other functions
}
}
It's been stumping me for hours now! I feel like the solution is super simple but I cannot seem to wrap my head around it. I have tried to simply make the Tester() function the export, but am still having the same issue. Why can't I seem to yield to the Tester() function?
Also, what may an alternative be to this approach? I want to maintain the Object nature of the module so that the module can be loaded with different inputs, such as the s_id variable/object in the example above.
a Node module (using harmony) that upon loading by another module/application, has to be yielded to so that things in it's construct can be executed and loaded before any of it's exposed functions can be called
Don't do that. Generators are not made for asynchrony. yield doesn't do what you want here. A module is not "yielded" to await something in it to load. yield is magic, but not async magic.
If you must use an asynchronous module loading process, export a promise for your actual module. That is a standard interface for something to await, and can be consumed using a standardized approach that does not rely on internals of your module.
You still can use yield syntax for constructing that promise, just use your favorite coroutine library.
return yield new Tester();
…
function* Tester(){…}
Uh oh. Well yes, apparently it is possible to call generator functions as constructors. But believe me, it is not what you want. A constructor for an arbitrary object should return that object, instead of an iterator (just like it shouldn't return a promise). If some of your object methods are generator methods, that's fine, but your constructor should be not.
If you really want to use a generator function in your code like that (and it's not meant to be a constructor), you
will need to yield* the iterator you've created (tester()) instead of yielding it
must not overwrite its .prototype property like you did, as that causes the iterator to malfunction. (Of course you should never do that at all, even though most of the time it works)
I am developing a Node.js extension and I would like to pass a javascript function into it that can be called later from the c++ code. If I store the javascript function in my wrapper class and call it right when it is passed in it works fine, however if I store it and try to call it later i get the following error:
node: /home/david/.node-gyp/0.10.28/src/node_object_wrap.h:61: static T*
node::ObjectWrap::Unwrap(v8::Handle<v8::Object>) [with T = Queue]: Assertion
`handle->InternalFieldCount() > 0' failed.
I am trying to accomplish something like
var callback = require('my_addon');
callback.setCallback(function(){ console.log("test"); } // works fine if i call the function fron setCallback
callback.callCallback(); // gives that error if i try calling it from here
Does anyone know if this is possible in Node.js?
Got it, you have to store the value in a Persistent rather than a Local