Code :
Model = {};
var functionToTest = function (dataPoints) {
return Model.getDataPointValue(dataPoints, function (dataPoint) {
if (dataPoint.unit === 'unit') {
return dataPoint.value;
}
});
};
Model.functionToTest = functionToTest;
Test Case :
describe ('functionToTest', function () {
it ('validates correct value is returned', function () {
var dataPoints = [{
'unit': 'unit',
'value' : 1
}];
var mock = sinon.mock(Model);
mock.expects('getDataPointValue').once().withExactArgs(dataPoints, function (dataPoint) {
if (dataPoint.unit === 'unit') {
return dataPoint.value;
}
});
Model.functionToTest(dataPoints);
mock.verify();
});
});
Response :
ExpectationError: Unexpected call: getDataPointValue([{ unit: "unit", value: 1 }], function () {})
Expected getDataPointValue([{ unit: "unit", value: 1 }], function () {}) once (never called)
I cant figure out why it wouldnt accept the same function definition as a parameter. Any clues ? Is there a different way to test function parameters being functions in mocha using sinon ?
If you are always passing the same function to the getDataPointValue, there's no need to test if that function was passed, you can test only if the first argument was passed like this: