Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stubbing a class method with Sinon.js

I am trying to stub a 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 mocking a 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 (http://jsfiddle.net/pebreo/wyg5f/5/) for the above code, and the jsFiddle for the SO question that I mentioned (http://jsfiddle.net/pebreo/9mK5d/1/).

I made sure to include sinon in the External Resources in jsFiddle and even jQuery 1.9. What am I doing wrong?

like image 879
Paul Avatar asked Jan 12 '14 06:01

Paul


People also ask

How do you stub the instance of a class in Sinon?

to stub the myMethod instance method in the YourClass class. We call callsFake with a callback that returns the value we want for the fake method. sinon. stub(YourClass, "myStaticMethod").

How do you stub property of a class?

If you want to stub the property of an object, use the value() method of the Stub .

What is stub method in JavaScript?

What are Stubs? A test stub is a function or object that replaces the actual behavior of a module with a fixed response. The stub can only return the fixed response it was programmed to return.

How do I stub the whole class in Sinon?

To stub the whole class: var WrapperStub = sinon.spy (function () { return sinon.createStubInstance (Wrapper); }); sinon.createStubInstance will create an instance of Wrapper where every method is a stub. sinon.spy will allow us to spy the class instantiation. So you could exercise it like this:

How to use Sinon to stub a dependency in a test?

Though in some more basic cases, you can get away with only using Sinon by modifying the module exports of the dependency. 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.

What is the advantage of stubbing methods in Java?

Stubbing individual methods tests intent more precisely and is less susceptible to unexpected behavior as the object’s code evolves. If you want to create a stub object of MyConstructor, but don’t want the constructor to be invoked, use this utility function.

What does it mean to stub a method?

Stubs all the object’s methods. Note that it’s usually better practice to stub individual methods, particularly on objects that you don’t understand or control all the methods for (e.g. library dependencies). Stubbing individual methods tests intent more precisely and is less susceptible to unexpected behavior as the object’s code evolves.


2 Answers

Your code is attempting to stub a function on Sensor, but you have defined the function on Sensor.prototype.

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

is essentially the same as this:

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

but it is smart enough to see that Sensor["sample_pressure"] doesn't exist.

So what you would want to do is something like these:

// Stub the prototype's 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's logic. var sensor = sinon.createStubInstance(Sensor); console.log(sensor.sample_pressure()); 
like image 195
loganfsmyth Avatar answered Sep 21 '22 06:09

loganfsmyth


The top answer is deprecated. You should now use:

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

Or for static methods:

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

Or for simple cases just use returns:

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({}) 
like image 39
danday74 Avatar answered Sep 20 '22 06:09

danday74