javascript - 如何在 Javascript 中为服务器的 AjaxCall 编写测试方法?

标签 javascript asp.net-mvc-3 visual-studio-2010 qunit chutzpah

下面给出了有效的ajax调用代码

$('#callControllerBtn').click(function () {
    currentlySelectedRow = grid.select();
    sendProductIDToController(currentlySelectedRow);
});

function sendProductIDToController(currentlySelectedRow) {
    $.ajax({
        url: "Home/sendProductID/", // Home = Controller , sendProductID = Action
        data: {
            ID: pID
        },
        cache: false,
        type: "POST",
        timeout: 10000,
        dataType: "json",
        success: function (result) {
            if (result) {
                alert("Successfully Completed");
                grid.removeRow(currentlySelectedRow);
                editor.value("");
            } else {
                alert("Failed");
            }
        }
    });
}

现在我想编写相同的代码,但使用 JS 测试方法。我在 vs 2010 中使用 QUnit、ChutzPah,让我看看模拟/假 ajax 调用也用测试方法实现工作代码的好做法是什么。

最佳答案

http://net.tutsplus.com/tutorials/javascript-ajax/how-to-test-your-javascript-code-with-qunit/

查看异步章节。

您可以在 $.ajax() 对象文字参数中使用 JQuery 固定属性来指定 ajax 调用应重新路由到的静态文件。 JMVC 文档中对装置有很好的解释:

http://javascriptmvc.com/docs.html#!jQuery.fixture

关于javascript - 如何在 Javascript 中为服务器的 AjaxCall 编写测试方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14588248/

相关文章:

javascript - 日期选择器和图像 slider 冲突

asp.net-mvc - 为 ActionLink 分配图标而不是文本

c# - 在整个 Windows 窗体应用程序生命周期中只运行一次代码

visual-studio-2010 - Resharper - 在构建时运行 NUnit 单元测试

c++ - Visual Studio 2010 像 Visual Studio 6 一样在 C++ 中编译内联程序集?

javascript - 如何使用 props 导出 React 组件?

javascript - 如何为<select>标签添加水平和垂直滚动条

javascript - 在某个滚动点开始一个页面

asp.net - ASP.NET_SessionId与.ASPXAUTH为什么我们都需要它们?

asp.net-mvc - 使用 CheckBoxFor