Stubbing a Mongoose model with Sinon

There are two ways to accomplish this. The first is var mongoose = require(‘mongoose’); var myStub = sinon.stub(mongoose.Model, METHODNAME); If you console log mongoose.Model you will see the methods available to the model (notably this does not include lte option). The other (model specific) way is var myStub = sinon.stub(YOURMODEL.prototype.base.Model, ‘METHODNAME’); Again, the same methods … Read more

Stubbing a class method with Sinon.js

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 … Read more

How to mock localStorage in JavaScript unit tests?

Here is a simple way to mock it with Jasmine: let localStore; beforeEach(() => { localStore = {}; spyOn(window.localStorage, ‘getItem’).and.callFake((key) => key in localStore ? localStore[key] : null ); spyOn(window.localStorage, ‘setItem’).and.callFake( (key, value) => (localStore[key] = value + ”) ); spyOn(window.localStorage, ‘clear’).and.callFake(() => (localStore = {})); }); If you want to mock the local storage … Read more