Stub Ajax response in Jasmine 2.0

191 views
Skip to first unread message

g.$hrikant

unread,
Mar 1, 2016, 3:54:29 PM3/1/16
to Jasmine

For Jasmine unit testing when I call processDeviceConversionReq(), Internally it will call three ajax call and further process on these three responses(ie. jsp1,jsp2,jsonResponse).

So, how can I stub the responses in this condition.

I have the instance of ajax as :

 

spyOn($,"ajax").and.callFake(function(e) {

       

});

 


 

function processDeviceConversionReq(lrdSkuMap){

.

.

 

$.when(getsubmitProgressBarJsp(), getDeviceConvertCompleteJsp(), initiateDeviceConversionService(serviceUrl,header,conversionPayload,"POST")).done(function(jsp1,jsp2,jsonResponse){…}

.

.

}

 
 
1.  function getsubmitProgressBarJsp()
{
  return $.ajax({
         type : "POST",
         url : "submitProgressBar",
         timeout : 1000 * 60 * 5,
         cache : false,
         error : function() {
         },
         success : function(jsp1) {
                 return true;
         }
  });
}
2.  function getDeviceConvertCompleteJsp()
{
  return $.ajax({
         type : "POST",
         url : "getDeviceConvertComplete",
         timeout : 1000 * 60 * 5,
         cache : false,
         error : function() {
         },
         success : function(jsp2) {
                 return true;
         }
  });
}
3.  function initiateDeviceConversionService(serviceUrl,header,conversionPayload,methodType)
{
  return $.ajax({
         url:serviceUrl,
         type:methodType,
         contentType:'application/json',
         headers:header,
         data:JSON.stringify(conversionPayload),
         dataType:'json',
         error : function() {
         },
         success : function(responseData) 
         {
                 return true;
         }
  });
}

Gregg Van Hove

unread,
Mar 1, 2016, 4:12:50 PM3/1/16
to jasmi...@googlegroups.com
Take a look at Jasmine-Ajax https://github.com/jasmine/jasmine-ajax or something similar. It will let you provide fake responses for Ajax requests and validate that you're making the requests you need to. 

- Gregg 
--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at https://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

Steve Yoo

unread,
Feb 28, 2018, 11:09:00 AM2/28/18
to Jasmine
@Gregg, does jasmine-ajax work with the latest angular cli projects?
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+unsubscribe@googlegroups.com.

Gregg Van Hove

unread,
Feb 28, 2018, 12:49:03 PM2/28/18
to jasmi...@googlegroups.com
Theoretically it should, but it might depend on how angular cli expects things to be loaded up. Jasmine-Ajax really expects to be in a browser, but it just replaces the XMLHttpRequest constructor on the global object when installed, so there might be a bit more boot strapping to make sure that is available to be replaced.

Hope this helps. Thanks for using Jasmine!

-Gregg
Reply all
Reply to author
Forward
0 new messages