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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With