如何使用mocha测试jquerypost方法

How to use mocha to test jquery post method

我使用 jQuery 将表单信息发送到我的服务器 php API。现在我需要为它编写一个 mocha 测试,我对 mocha 和 jQuery 真的很陌生。我需要做什么?这是我的代码:

<form class="form" method="post" action="http://140.115.xxx.xx/FinalProject/FetchUserData.php">
    <input type="text" placeholder="Username" name ="username" id="username">
    <input type="password" placeholder="Password" name ="password">
    <button type="submit" id="login-button">Login</button><br>
    <li id="showerror"></li>
    <a id="goToRegister" href="register.html" >Register</a>
</form>
$(document).ready(function(){
    var $form = $('form');
    $form.submit(function(){
        $.post($(this).attr('action'), $(this).serialize(), function(response){
            if(response == true) {
                var username = document.getElementById('username').value;
                window.location.href="main.html?" + username;
            } else {
                document.getElementById('showerror').innerHTML = "invalid username or password";
            }
        }, 'json');
        return false;
    });
});

您可以使用一个很棒的模拟工具 sinon, it has Fake XMLHttpRequest