Setting up QUnit tests for AJAX calls

感情迁移 提交于 2020-07-09 16:13:59

问题


I am attempting to use Q-Unit Testing for my code. I would like to run some tests against my AJAX queries to ensure they are running ok and returning what they should.

Is there anyway I could get guidance on the two ajax calls below.

The first AJAX savePgID() essentially takes in 2 numbers and then goes to the pagaAJAX.php page where a simple MySQL INSERT query runs to insert those 2 numbers into the DB.

The second AJAX getModuleData() call again goes to a PHP script where a SELECT query runs, which GETS data from the database and the AJAX just pushes the content to certain divs.

My 2 AJAX calls inside of script.js:

function savePgID(moduleID, pageID){
    $.ajax({  
        url: "pageAJAX.php",  
        method: "POST",   
        data: {moduleID:moduleID, pageID:pageID},
        dataType: 'json', //ajax to expect JSON data type
      });
  }

function getModuleData(moduleID, pageID){
  console.log
  console.log(moduleID);
  console.log(pageID);
  $.ajax({  
      url: "moduleAJAX.php",  
      method: "POST",   
      data: { moduleID:moduleID, pageID:pageID },
      dataType: 'json', //ajax to expect JSON data type
      success: function(data){  
        //console.log(data);
            $('#result').html(data.result);    
            $('#modContent').html(data.modContent);  
            $('#modTitle').html(data.modTitle); 
            $('#modID').html(data.modID);
            $('#pgID').html(data.pgID);
            $('#modProgress').html("Page " + data.pgID); // For footer of modal overlay
      }  
  });
}

my test.html page:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Q-Unit Tests</title>
    <link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.10.0.css">


</head>
<body>

    <div id="qunit"></div>
    <div id="qunit-fixture"></div>
    <script src="https://code.jquery.com/qunit/qunit-2.10.0.js" integrity="sha256-X1fQXHSYGxa4V2bqkEAQW0DQGSxJrKveasahr959o28=" crossorigin="anonymous"></script>

    <script src="script.js"></script>

</body>
</html>

来源:https://stackoverflow.com/questions/61682131/setting-up-qunit-tests-for-ajax-calls

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!