Search code examples
unit-testingqunitsapui5stubbing

how to stub a function that returns an object in QUnit?


I have the following lines in my SAPUI5 app

var dateVal = controls.awardDate.getDateValue();
var month = dateVal.getMonth();

awardDate is a datepicker the user enters a date on and returns a javascript date object. This is a snippet of my qunit to test this element.

awardDate: {
  getValue: getInvalidValue,
  getValueState: getValueStateWarning,
  setValue: setValue,
  getDatevalue: getDateValue
 }

In my qunit I get an error saying that the object doesn't support property or method 'getDateValue'. I'm not sure how I'm supposed to stub this function when it returns an object. Other tests I have do it this way

var getValue = sinon.stub().returns('');

where I get an empty string. so my attempt at to do it with the datepicker is

var getDateValue = sinon.stub().returns(new Date());

but this doesn't work. I still get the same error. Has anyone done this before?

edit/update: I was able to fix part of the problem by doing the following

var getValueDate = sinon.stub().returns(Object, function(){ });

Now the problem I have is the same error but for getMonth() which returns a string. All other variables are global but dateVal is created on the spot when the user updates the datepicker. Any ideas on how to proceed on this one?


Solution

  • I was able to figure out how to solve this. I had to make the Object type a specific Date object like this

     var getValueDate = sinon.stub().returns(new Date()), function(){ });