Sinon stubbing a function passed as a parameter

Jorayen

I have the following Example class:

function Example() {...}
Example.prototype.someFunc1() {...}
Example.prototype.someFunc2() {...}
Example.prototype.func(func) {var res = func(); ...}

I usually call Example#func() as follows:

var example = new Example();
example.func(example.someFunc1)
// or like this, depending on what I want
example.func(example.someFunc2)

Now I stub Example#someFunc1() as follows in my test:

var example = new Example();
sinon.stub(example, 'someFunc1').returns(...);
exmaple.func(example.someFunc1);

Problem is that Example#someFunc1() is not being stubbed this way and being called normally. What can I do in such situation ?

Dave Newton

In your example you save a reference to the function. Then you stub it.

You're passing a reference to the original function, not the stubbed function.

The function you stub does not disappear when you stub it–that's why you can restore() it later. You either need to pass a reference to object's function itself, e.g.,

sinon.stub(example, 'opt1').returns(42);
example.logic([3, 2], example.opt1);

Or pass a reference to the stub, e.g.,

var fn = sinon.stub(example, 'opt1').returns(42);
example.logic([3, 2], fn);

The latter doesn't really make any sense as a test, though; you could just pass in any function, there's no reason to stub anything.

FWIW, your fiddle is nowhere near equivalent to the original code you posted.


It's unclear what you're trying to test: you pass a function reference–this could be any old function, whether or not it's attached to the Example object, e.g., an anonymous function would be fine.

If the function under test itself called the stubbed function, stubbing makes sense.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sinon stubbing a function passed as a parameter

From Dev

Stubbing a promisified function with sinon and bluebird

From Dev

Sinon anonymous stub 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

Stubbing a prototype method with sinon

From Dev

Calling a function passed as parameter

From Dev

Lambda function passed as parameter

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

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 Dev

What is actually passed as a parameter to the function?

From Dev

JQuery Function with parameter passed Not Working

From Dev

Print name if parameter passed to function

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

From Dev

Sinon.stub not stubbing private method

From Dev

Adding Dynamic Parameter to a function with parameter which is passed as parameter to current function

Related Related

  1. 1

    Sinon stubbing a function passed as a parameter

  2. 2

    Stubbing a promisified function with sinon and bluebird

  3. 3

    Sinon anonymous stub 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

    Stubbing a prototype method with sinon

  9. 9

    Calling a function passed as parameter

  10. 10

    Lambda function passed as parameter

  11. 11

    Mocking / stubbing mongoose findById with sinon

  12. 12

    Stubbing a get method using Sinon

  13. 13

    Stubbing a React component method with Sinon

  14. 14

    stubbing an entire class for testing in sinon

  15. 15

    Stubbing Backbone listenTo callbacks with sinon

  16. 16

    Mocking / stubbing mongoose findById with sinon

  17. 17

    stubbing method with async callback in sinon

  18. 18

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

  19. 19

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

  20. 20

    What is actually passed as a parameter to the function?

  21. 21

    JQuery Function with parameter passed Not Working

  22. 22

    Print name if parameter passed to function

  23. 23

    Stubbing a class method with Sinon.js

  24. 24

    Stubbing stripe with sinon - using stub.yields

  25. 25

    sinon.stub not stubbing original method

  26. 26

    Stubbing window.location.href with Sinon

  27. 27

    Stubbing Backbone Class constructor with Jasmin and Sinon

  28. 28

    Sinon.stub not stubbing private method

  29. 29

    Adding Dynamic Parameter to a function with parameter which is passed as parameter to current function

HotTag

Archive