I am new to Sinon mocking and trying to test a function
so my function is something like:
`save: function (data) {
var x = 10;
var y = [];
Deferred.when(
app.ajax({
type: 'GET',
url: 'someurl/x/endpoint',
success: function (response) {
console.log("respsonse", response);
if (!response.error) {
y.push(response);
}
}
})
).then(function (response1) {
console.log("Printing 1");
console.log("response1", response1);
});
}`
And test is:
`var x= {
“ID”:”10″
}
var data = {
“ID”:”10″
}
server.respondWith(‘GET’, “someurl/x/endpoint”, [
200,
{ ‘Content-Type’: ‘application/json’ },
JSON.stringify(x)
]);
save(data);`
The issue is when I am running the test I get
Printing 1
response1 undefined
ERROR: TypeError: undefined is not an object (evaluating ‘response1’)
I am using Sinon to mock and maybe going wrong somewhere.
I am trying to mock the GET AJAX call in order to take its response and proceed further.
Please can anyone guide me what’s the appropriate way to do so?
Anmol Jaising is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.