8

This is jquery function:

$.get("/url", function(){

   //success
}).fail(function(){
  //fail     <---- how to make code go in there.
});

Problem is how to let program goes into .fail block, I use .Net MVC, However, set break point in Controller doesn't trigger a timeout exception then leads to fail callback.

Don't know how people test this.

Should I start looking at some tools ?

CodeFarmer
  • 2,644
  • 1
  • 23
  • 32
  • 2
    You're looking for what is called a "mock" library - e.g. https://github.com/jakerella/jquery-mockjax - most testing libraries will have a way to mock http requests built in( see http://stackoverflow.com/questions/13148356/how-to-properly-unit-test-jquerys-ajax-promises-using-jasmine-and-or-sinon for an example) – Sacho Apr 24 '15 at 08:33
  • 1
    Which conditions , responses should call `.fail()` ? Tried utilizing `.always()` , defining "success" , "fail" conditions for all responses within `.always()` ? – guest271314 Apr 24 '15 at 09:10

2 Answers2

1

Put this in your Controller method:

return new HttpStatusCodeResult(HttpStatusCode.NotFound);
Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
  • In any recent version of MVC, just `return HttpNotFound();` https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.httpnotfound%28v=vs.118%29.aspx` – iCollect.it Ltd Apr 24 '15 at 08:34
0

Use an invalid endpoint URL. You should get 404 - not found http status code error.

Vlad
  • 3,346
  • 2
  • 27
  • 39