Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8534795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:16:44+00:00 2026-06-11T10:16:44+00:00

Here is the code about the javascript submit request (1). Here is the test

  • 0

Here is the code about the javascript submit request (1).
Here is the test about mocking the ajax request by using jasmine (2).

I would like to mock the server behaviour. Any ideas?
See the comment in (1) and (2) for more details.

P.S.:
Actually in both case the done and the fail Deferred Object of fakeFunction are called.


(1)

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;
    });
};

(2)

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?
        });
    });

});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:16:45+00:00Added an answer on June 11, 2026 at 10:16 am

    We actually ran into the same problem, trying to test Deferred objects that represent AJAXed template scripts for on-the-fly templating. Our testing solution involves using the Jasmine-Ajax library in conjunction with Jasmine itself.

    So probably it will be something like this:

    describe('When Submit button handler fired', function () {
      jasmine.Ajax.useMock();
    
      describe('if the message is empty', function () {
    
        beforeEach(function() {
          spyOn(backendController, 'submitForm').andCallThrough();
          // replace with wherever your callbacks are defined
          spyOn(this, 'onSuccess');
          spyOn(this, 'onFailure');
          this.view.$el.find('#message').text('');
          this.view.$el.find('form').submit();
        });
    
        it('backendController.submitForm and fail Deferred Object should be called', function () {
          expect(backendController.submitForm).toHaveBeenCalledWith('');
          mostRecentAjaxRequest().response({
            status: 500, // or whatever response code you want
            responseText: ''
          });
    
          expect( this.onSuccess ).not.toHaveBeenCalled();
          expect( this.onFailure ).toHaveBeenCalled();
        });
    });
    

    Another thing, if you can, try to break up the functionality so you’re not testing the entire DOM-to-response-callback path in one test. If you’re granular enough, you can actually test asynchronous Deferred resolutions by using Deferred objects themselves inside your tests!

    The key is to actually use Deferred objects within your tests themselves, so that the scope of the expect call is still within your it function block.

    describe('loadTemplate', function() {
      it('passes back the response text', function() {
        jasmine.Ajax.mock();
        loadTemplate('template-request').done(function(response) {
          expect(response).toBe('foobar');
        });
        mostRecentAjaxRequest().response({ status:200, responseText:'foobar' });
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about Ajax form submit. Here is the code: Controller [HttpPost,
There are a lot of posts on here about this, I'm using this code
Please see working code here: http://jsfiddle.net/hrnDG/6/ I would like to modify this code so
The code here is X++. I know very little about it, though I am
I am sorry about the amount of code here. I have tried to show
i just want to ask about my html code here, i have a bit
Yesterday I posted a question here ( FxCop and Code Analysis Rules ) about
I want to ask about strcpy. I got problem here. Here is my code:
I've seen other questions here about PNS, in terms of code, but how does
I went to Twitter's resource page here (https://twitter.com/about/resources/tweetbutton) and got the following code: <a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.