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());
loganfsmyth Jan 12 '14 at 19:47 2014-01-12 19:47
source share