title: How to test the done and fail Deferred Object by using jasmine
body: <p>Here is the code about the javascript submit request (1).<br>
  Here is the test about mocking the ajax request by using jasmine (2).</p>
  
  <p>I would like to mock the server behaviour. Any ideas?<br>
  See the comment in (1) and (2) for more details.    </p>
  
  <p>P.S.:<br>
  Actually in both case the done and the fail Deferred Object of fakeFunction are called.</p>
  
  <hr>
  
  <p>(1)</p>
  
  <pre><code>submitForm: function () {
       // the server execute fail only if message.val() is empty
       // and I would like to mock this behaviour in (2)
       backendController.submitForm(message.val()).done(this.onSuccess).fail(this.onError);
  },
  
  backendController.submitForm = function (message) {
      return $.ajax({
          url: 'some url',
          type: 'POST',
          dataType: 'json',
          data: {
              message: message
          }
      }).done(function () {
          //some code;
      });
  };
  </code></pre>
  
  <hr>
  
  <p>(2)</p>
  
  <pre><code>describe('When Submit button handler fired', function () {
      var submitFormSpy,
          fakeFunction = function () {
              this.done = function () {
                  return this;
              };
              this.fail = function () {
                  return this;
              };
              return this;
          };
  
      beforeEach(function () {
          submitFormSpy = spyOn(backendController, 'submitForm').andCallFake(fakeFunction);
      });
  
      describe('if the message is empty', function () {
          beforeEach(function () {
              this.view.$el.find('#message').text('');
              this.view.$el.find('form').submit();
          });
          it('backendController.submitForm and fail Deferred Object should be called', function () {
              expect(submitFormSpy).toHaveBeenCalled();
              // how should I test that fail Deferred Object is called?
          });
      });
  
      describe('if the message is not empty', function () {
          beforeEach(function () {
              this.view.$el.find('#message').text('some text');
              this.view.$el.find('form').submit();
          });
          it('backendController.submitForm should be called and the fail Deferred Object should be not called', function () {
              expect(submitFormSpy).toHaveBeenCalled();
              // how should I test that fail Deferred Object is not called?
          });
      });
  
  });
  </code></pre>