Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating request stub with sinon in mocha

Tags:

I'm using mocha to test some classes and I need to create a stub of request library.

I'm using sinon, and I'm able to create a stub of the request.get method but I'm not able to create a stub of the request method (the http calls try to connect to a server). As I have read, request.get is an alias for request but when I stub request.get it has no effect over request calls.

This code works (using request.get):

In tests:

request = require 'request'  describe "User test", ->   user = {}   before (done) ->     user = new test.user('Ander', 18)         sinon.stub(request, 'get').yields(null, {statusCode: 200}, 'foo')             done()   after (done) ->     request.get.restore()     done()   it "testing server response", ->     user.getData().should.equal 'ander' 

In source:

request = require 'request'  class User   contructor(@name, @age): ->   getData: ->     mydata = ''     request.get 'http://127.0.0.1:8080/', (err, response, body) ->               if not err and response.statusCode == 200         mydata = body       else         err = throw new Error "Errorea"     mydata 

But this doesn't work (tries to connect to the supplied url):

In tests:

request = require 'request'  describe "User test", ->   user = {}   before (done) ->     user = new test.user('Ander', 18)         sinon.stub(request, 'Request').yields(null, {statusCode: 200}, 'foo')     #Creating the stub this way, doesn't work neither     #sinon.stub(request, 'get').yields(null, {statusCode: 200}, 'foo')     done()   after (done) ->     request.Request.restore()     done()   it "testing server response", ->     user.getData().should.equal 'ander' 

In source:

request = require 'request'  class User   contructor(@name, @age): ->   getData: ->     mydata = ''     request 'http://127.0.0.1:8080/', (err, response, body) ->               if not err and response.statusCode == 200         mydata = body       else         err = throw new Error "Errorea"     mydata 

Which is the right way to create a stub for request call? Which is the method to be stubed?

like image 206
Ander2 Avatar asked Nov 18 '13 14:11

Ander2


People also ask

How do I stub an object in Sinon?

If you need to check that a certain value is set before a function is called, you can use the third parameter of stub to insert an assertion into the stub: var object = { }; var expectedValue = 'something'; var func = sinon. stub(example, 'func', function() { assert. equal(object.

How does Sinon stub work?

To stub a dependency (imported module) of a module under test you have to import it explicitly in your test and stub the desired method. For the stubbing to work, the stubbed method cannot be destructured, neither in the module under test nor in the test.

How do you stub a promise in Sinon?

To stub a promise with sinon and JavaScript, we can return a promise with a stub. import sinon from "sinon"; const sandbox = sinon. sandbox. create(); const promiseResolved = () => sandbox.


1 Answers

Although request is a great library, it is not a good example of well structured API. And because module request is defined as a function with additional methods (similarly like express), as what I know you can't create stub for function request with sinon.

The best thing you can do is to avoid to use request function in your code and use only request.get, request.post, etc., which you can easily stub.

Creating stub for Request in your second example doesn't help because Request is not a method, see source code.

like image 169
ivoszz Avatar answered Oct 15 '22 17:10

ivoszz