Sinon - how to drown out a nested function?

Sorry if this is a simple question, I'm relatively new to Node and Sinon. I am trying to figure out how to claim that a nested asynchronous function was called in Nodejs.

I use mocha, chai, sinon and request ( https://github.com/request/request ), but I think I'm missing something basic on the stubbing part.

An example inside my_app.js is

var request = require('request'); function MyModule() { }; MyModule.prototype.getTicker = function(callback) { request('http://example.com/api/ticker', function(error, response) { if (error) { callback(error); } else { callback(null, response); } }); }; exports.mymodule = new MyModule(); 

Inside the test. I am trying to drown out the call for the request and provide some dummy data to return. But I continue to receive the error message "request not defined" in the line where I create the stub.

 var myApp = require('../my_app.js') ,assert = require("assert") ,chai = require('chai') ,sinon = require('sinon') ,expect = chai.expect; describe('mymodule object', function() { var mymodule = myApp.mymodule; before(function(done) { sinon.stub(request).yields(null, JSON.stringify({ price: '100 USD' })); done(); }); it('getTicker function should call request on example ticker', function(done) { mymodule.getTicker(function(error, result){ request.called.should.be.equal(true); done(); }); }); }); 

I know that I can assign sinon.stub (objname, "funcname") or sinon.stub ("funcname"), but they only set the external object, I'm trying to stub the request for the function that is inside the getTicker function.

Any ideas on how to do this? Maybe I also need to use a spy (but how?) Or is there a better approach to testing the getTicker function above?

+5
source share
1 answer

You get an undefined message because the request variable is unknown in your test. However, even if you have to fix this and assign the request library to this variable, you still get an error, since sinon requires a method for any object provided to create a stub.

The consequence of this is that the request function itself cannot be sharpened, since it does not exist on the object, but as a function on which other methods are defined. Therefore, in order to maintain testability, it would be preferable to refuse to use request directly in your code and instead use your own applied methods, which can then be drowned out. For instance:

my_app.js

 MyModule.prototype.getTicker = function(callback) { request.get('http://example.com/api/ticker', function(error, response) { ... }); }; 

my_test.js

 var request = require('request'); before(function() { sinon.stub(request, 'get').yields(null, JSON.stringify({price: '100 USD'})); }); it('getTicker function should call request on example ticker', function() { mymodule.getTicker(); request.get.called.should.be.equal(true); }); 

(Note that running mocha is not needed asynchronously when the loop is synchronous).

+6
source

Source: https://habr.com/ru/post/1208964/


All Articles