Stubbing class method with Sinon.js

I try to stub the method using sinon.js, but I get the following error:

Uncaught TypeError: Attempted to wrap undefined property sample_pressure as function

I also went to this question ( Stubbing and / or mocked the class in sinon.js? ) And copied and pasted the code, but I get the same error.

Here is my code:

 Sensor = (function() { // A simple Sensor class // Constructor function Sensor(pressure) { this.pressure = pressure; } Sensor.prototype.sample_pressure = function() { return this.pressure; }; return Sensor; })(); // Doesn't work var stub_sens = sinon.stub(Sensor, "sample_pressure").returns(0); // Doesn't work var stub_sens = sinon.stub(Sensor, "sample_pressure", function() {return 0}); // Never gets this far console.log(stub_sens.sample_pressure()); 

Here is the jsFiddle code above ( http://jsfiddle.net/pebreo/wyg5f/5/ ) and jsFiddle for the SO question I mentioned ( http://jsfiddle.net/pebreo/9mK5d/1/ ).

I have included sine in External Resources in jsFiddle and even jQuery 1.9. What am I doing wrong?

+78
javascript sinon
Jan 12 '14 at 6:40
source share
5 answers

Your code tries to lock the function on Sensor , but you defined the function on Sensor.prototype .

 sinon.stub(Sensor, "sample_pressure", function() {return 0}) 

essentially coincides with this:

 Sensor["sample_pressure"] = function() {return 0}; 

but he's smart enough to see that Sensor["sample_pressure"] does not exist.

So what you want to do is something like this:

 // Stub the prototype function so that there is a spy on any new instance // of Sensor that is created. Kind of overkill. sinon.stub(Sensor.prototype, "sample_pressure").returns(0); var sensor = new Sensor(); console.log(sensor.sample_pressure()); 

or

 // Stub the function on a single instance of 'Sensor'. var sensor = new Sensor(); sinon.stub(sensor, "sample_pressure").returns(0); console.log(sensor.sample_pressure()); 

or

 // Create a whole fake instance of 'Sensor' with none of the class logic. var sensor = sinon.createStubInstance(Sensor); console.log(sensor.sample_pressure()); 
+132
Jan 12 '14 at 19:47
source share

The top answer is out of date. Now you should use:

 sinon.stub(YourClass.prototype, 'myMethod').callsFake(() => { return {} }) 

Or for static methods:

 sinon.stub(YourClass, 'myStaticMethod').callsFake(() => { return {} }) 

Or for simple cases, just use return:

 sinon.stub(YourClass.prototype, 'myMethod').returns({}) sinon.stub(YourClass, 'myStaticMethod').returns({}) 

Or, if you want to stub a method for an instance:

 const yourClassInstance = new YourClass(); sinon.stub(yourClassInstance, 'myMethod').returns({}) 
+31
Jul 22 '17 at 23:26
source share

I came across the same error trying to mock a CoffeeScript class method using Sinon.

For a class like this:

 class MyClass myMethod: -> # do stuff ... 

You can replace his method with a spy as follows:

 mySpy = sinon.spy(MyClass.prototype, "myMethod") # ... assert.ok(mySpy.called) 

Just replace spy with stub or mock as needed.

Please note that you need to replace assert.ok with any statement available in your testing framework.

+3
Jun 23 '17 at 14:14
source share

Thanks to @loganfsmyth for the tip. I was able to get the stub to work on an Ember class method as follows:

 sinon.stub(Foo.prototype.constructor, 'find').returns([foo, foo]); expect(Foo.find()).to.have.length(2) 
+2
Oct 24 '14 at 21:22
source share

test answer. sorry test answer. sorry test answer. sorry test answer. sorry test answer. I apologize. import code

0
Apr 17 '19 at 2:56
source share



All Articles