Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sinon stub not working with namespaced functions. #1472

Closed
samueljoli opened this issue Jun 26, 2017 · 3 comments
Closed

Sinon stub not working with namespaced functions. #1472

samueljoli opened this issue Jun 26, 2017 · 3 comments

Comments

@samueljoli
Copy link

samueljoli commented Jun 26, 2017

We understand you have a problem and are in a hurry, but please provide us with some info to make it much more likely for your issue to be understood, worked on and resolved quickly.

  • Sinon version : 1.x.x But confirmed that bug did exist in latest version
  • Environment :
  • Example URL :
  • Other libraries you are using:

What did you expect to happen?
For the function to be be stubbed

What actually happens
Nothing

How to reproduce

Describe with code how to reproduce the faulty behaviour,

//...my module.js 
const Constructor = require('./constructor') //...just exports a singleton

// Need to namespace some of my functions and retain the `this` context
Object.defineProperty(Constructor.prototype, 'es', {
  get: function() {
    return {
      method: require('./implementations/doesSomething.js').bind(this)
    }
  }
});

module.exports = Constructor;




//...testFile.js
const Constructor = require('./constructor');
const instance = new Constructor();
const sinon = require('sinon');

sinon.stub(instance.es, 'method', function() {
  return 'hijacked original method'
});

/* 
...some test cases 
Namespaced functions will not be stubbed, but functions not namespaced (directly defined on the prototype) are stubbed as I would expect
*/

or link to code on JSBin or similar

Really long code sample or stacktrace
If you need to provide a dump of a stack trace or
other lengthy material, such as 80 lines of example code,
please stuff it in a `<details>` tag such as this
to make the issue more readable. Thanks.
@fatso83
Copy link
Contributor

fatso83 commented Jun 27, 2017

namespacing is of course not the issue here. try defining your property without Object.defineProperty: it will still be namespaced, but suddenly it will be stubbable ... Check up on the default values for writeable and configurable

@samueljoli
Copy link
Author

@fatso83 Hey thanks for the suggestion the only problem is that the this context is being lost, I suspected that this would still be a bug because regardless of how I go about namespacing, I would assume Sinon given a path to a function it should be stubbed, no?

@fatso83
Copy link
Contributor

fatso83 commented Jun 27, 2017

As this is a pure usage question and not a bug with sinon I need to close this to keep noise/signal down, but I answered your question on Stack Overflow

@sinonjs sinonjs locked and limited conversation to collaborators Jun 27, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants