Stubbing a promisified function with sinon and bluebird

Nepoxx

In the file I would like to test, I have the following code:

var httpGet = Promise.promisify(require("request").get);
httpGet(endpoint, {
    auth: {bearer: req.body.access_token},
    json: true
})
    .then(...)

Now, in my tests, I want to make sure that httpGet was called once, and make sure the parameters are valid. Before being promisified, my test looked like this:

beforeEach(function () {
    request.get = sinon.stub()
        .yields(null, null, {error: "test error", error_description: "fake google error."});
});

afterEach(function () {
    expect(request.get).to.have.been.calledOnce();
    var requestArgs = request.get.args[0];
    var uri = requestArgs[0];

    expect(uri).to.equal(endpoint);
    //...
});

Unfortunately this no longer works when request.get is promisified. I tried stubbing request.getAsync instead (since bluebird appends "Async" to promisified functions), but that does not work either. Any ideas?

Esailija

Promise.promisify doesn't modify the object, it simply takes a function and returns a new function, it is completely unaware that the function even belongs to "request".

"Async" suffixed methods are added to the object when using promisify All

Promise.promisifyAll(require("request"));

request.getAsync = sinon.stub()
        .yields(null, null, {error: "test error", error_description: "fake google error."});

expect(request.getAsync).to.have.been.calledOnce();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bluebird: Get reference to original function that was promisified

From Dev

Sinon stubbing a function passed as a parameter

From Dev

Sinon stubbing a function passed as a parameter

From Dev

Sinon function stubbing: How to call "own" function inside module

From Dev

Sinon - stubbing function with callback - causing test method to timeout

From Dev

Stubbing a function in sinon to return different value every time it is called

From Dev

Stubbing a function in sinon to return different value every time it is called

From Dev

Results from Bluebird promisified library is returned as an error

From Dev

Stubbing a prototype method with sinon

From Dev

sinon stub with es6-promisified object

From Dev

sinon stub with es6-promisified object

From Dev

Thrown error in promisified ExpressJs function

From Dev

Simple chain of promisified function wrapper?

From Dev

Thrown error in promisified ExpressJs function

From Dev

Mocking / stubbing mongoose findById with sinon

From Dev

Stubbing a get method using Sinon

From Dev

Stubbing a React component method with Sinon

From Dev

stubbing an entire class for testing in sinon

From Dev

Stubbing Backbone listenTo callbacks with sinon

From Dev

Mocking / stubbing mongoose findById with sinon

From Dev

stubbing method with async callback in sinon

From Dev

How to console.log a promisified mongoose query *without* bluebird

From Dev

Node.js sinon stubbing a function in parallel executions causes failed tests

From Dev

Node.js sinon stubbing a function in parallel executions causes failed tests

From Java

Stubbing a class method with Sinon.js

From Dev

Stubbing stripe with sinon - using stub.yields

From Dev

sinon.stub not stubbing original method

From Dev

Stubbing window.location.href with Sinon

From Dev

Stubbing Backbone Class constructor with Jasmin and Sinon

Related Related

  1. 1

    Bluebird: Get reference to original function that was promisified

  2. 2

    Sinon stubbing a function passed as a parameter

  3. 3

    Sinon stubbing a function passed as a parameter

  4. 4

    Sinon function stubbing: How to call "own" function inside module

  5. 5

    Sinon - stubbing function with callback - causing test method to timeout

  6. 6

    Stubbing a function in sinon to return different value every time it is called

  7. 7

    Stubbing a function in sinon to return different value every time it is called

  8. 8

    Results from Bluebird promisified library is returned as an error

  9. 9

    Stubbing a prototype method with sinon

  10. 10

    sinon stub with es6-promisified object

  11. 11

    sinon stub with es6-promisified object

  12. 12

    Thrown error in promisified ExpressJs function

  13. 13

    Simple chain of promisified function wrapper?

  14. 14

    Thrown error in promisified ExpressJs function

  15. 15

    Mocking / stubbing mongoose findById with sinon

  16. 16

    Stubbing a get method using Sinon

  17. 17

    Stubbing a React component method with Sinon

  18. 18

    stubbing an entire class for testing in sinon

  19. 19

    Stubbing Backbone listenTo callbacks with sinon

  20. 20

    Mocking / stubbing mongoose findById with sinon

  21. 21

    stubbing method with async callback in sinon

  22. 22

    How to console.log a promisified mongoose query *without* bluebird

  23. 23

    Node.js sinon stubbing a function in parallel executions causes failed tests

  24. 24

    Node.js sinon stubbing a function in parallel executions causes failed tests

  25. 25

    Stubbing a class method with Sinon.js

  26. 26

    Stubbing stripe with sinon - using stub.yields

  27. 27

    sinon.stub not stubbing original method

  28. 28

    Stubbing window.location.href with Sinon

  29. 29

    Stubbing Backbone Class constructor with Jasmin and Sinon

HotTag

Archive