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

  • SEARCH
  • Home
  • 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 8709009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:15:43+00:00 2026-06-13T04:15:43+00:00

I’m trying to test some jQuery ajax code using QUnit and Mockjax and have

  • 0

I’m trying to test some jQuery ajax code using QUnit and Mockjax and have it return different JSON for different tests, like this:

$(document).ready(function() {

function functionToTest() {
    return $.getJSON('/echo/json/', {
        json: JSON.stringify({
            "won't": "run"
        })
    });
}

module("first");

test("first test", function() {

    stop();

    $.mockjax({
        url: '/echo/json/',
        responseText: JSON.stringify({
            hello: 'HEYO!'
        })
    });

    functionToTest().done(function(json) {
        ok(true, json.hello);
        start();
    });               
});


 test("second test", function() {

    stop();

    $.mockjax({
        url: '/echo/json/',
        responseText: JSON.stringify({
            hello: 'HELL NO!'
        })
    });


    functionToTest().done(function(json) {
        ok(true, json.hello);
        start();
    });


});

});

Unfortunately it returns the same response for each call, and order can’t be guaranteed, so was wondering how I could set it up so that it was coupled to the actual request and came up with this:

$.mockjax({
    url: '/echo/json/',
    response: function(settings) {

        if (JSON.parse(settings.data.json).order === 1) {
            this.responseText = JSON.stringify({
                hello: 'HEYO!'
            });
        } else {
            this.responseText = JSON.stringify({
                hello: 'HELL NO!'
            });
        }
    }
});

This relies on parameters being sent to the server, but what about requests without parameters, where I still need to test different responses? Is there a way to use QUnit’s setup/teardown to do this?

  • 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-13T04:15:45+00:00Added an answer on June 13, 2026 at 4:15 am

    It looks like you need to call $.mockjaxClear(); before you create another mock handler.

    Mockjax works by changing the $.ajax method.

    At the bottom of its source code we can see that the $.mockjax method you are using, which is one of its exposed methods, just appends more handlers to the mockHandlers array.

        $.mockjax = function(settings) {
            var i = mockHandlers.length;
            mockHandlers[i] = settings;
            return i;
        };
    

    In the source code for the $.ajax replacement, we can see:

        // Iterate over our mock handlers (in registration order) until we find
        // one that is willing to intercept the request
        for(var k = 0; k < mockHandlers.length; k++) {
    

    Your problem is due to the $.ajax method being satisfied with the first handler (therefore not the latest) mock handler in the array for the /echo/json/ url.

    Here is my fork of your fiddle, just adding a $.mockjaxClear() line.


    Edit:

    A more flexible solution:

    • Outside any test function, decare a variable which will have the ID of the request handler for later changes.
    • Before a module where you need to override a certain handler, declare a variable (no need for initial value) for holding a backup of the handler you want to modify.
    • Then in the module setup function use $.extend to copy the settings from $.mockjax.handler(<your handlerID variable>) to this backup. In the module’s teardown, use $.mockjaxClear(<your handlerID variable>) to delete the module, and then set <your handlerID variable> to something else.
    • Now you can override your handlers in the module setup and in the individual test functions.

    But don’t take my word for it. Check out the fiddle.

    It’s a lot more flexible like this. You can change that one handler and leave all the other handlers you might have intact.

    It does seem to be potentially error-prone, so I would advise to be careful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.