javascript - Mockjax 在同一个测试文件中两次?

标签 javascript unit-testing qunit mockjax

使用 Qunit 和 MockJax,我寻求进行两个测试,为了便于理解,在此处进行了简化。以下两个测试之一失败,大概是因为这两个测试并行运行,因此它们不会各自绕过 $.ajax()。 (唯一的区别是每个中的 responseText。)关于调整它以便通过以下两个测试的好方法的任何想法?

function testAjax() {
    return $.ajax({
        type: 'POST',
        dataType: 'json', 
        url: '/fakeservice/1',
        data: {'a':'b'}
    });
}

asyncTest("testAjax 1", function () {
    $.mockjax({
        url: '/fakeservice/1',
        type: 'POST',
        dataType: 'json',
        responseText: { 'name1': 'foo' }
    });

    testAjax().then(
        function (response) {
            deepEqual(response.name1, 'foo', "no name1");
            start();
        },
        function (error) {
            ok(false, "got AJAX error");
            start(); 
        }
    );
});


asyncTest("testAjax 2", function () {
    $.mockjax({
        url: '/fakeservice/1',
        type: 'POST',
        dataType: 'json',
        responseText: { 'name1': 'bar' }
    });

    testAjax().then(
        function (response) {
            deepEqual(response.name1, "bar", "no name1");
            start();
        },
        function (error) {
            ok(false, "got AJAX error");
            start();
        }
    );
});

最佳答案

您必须在每次测试结束时调用 $.mockjaxClear()(例如,在您模块的 teardown() 方法中)。这会破坏模拟并为下一次测试准备环境。

function testAjax() {
    return $.ajax({
        type: 'POST',
        dataType: 'json', 
        url: '/fakeservice/1',
        data: {'a':'b'}
    });
}

module("AJAX tests", {
    teardown: function() {
        $.mockjaxClear();
    }
});
asyncTest("testAjax 1", function () {
    $.mockjax({
        url: '/fakeservice/1',
        type: 'POST',
        dataType: 'json',
        responseText: { 'name1': 'foo' }
    });

    testAjax().then(
        function (response) {
            deepEqual(response.name1, 'foo', "no name1");
            start();
        },
        function (error) {
            ok(false, "got AJAX error");
            start();
        }
    );
});


asyncTest("testAjax 2", function () {
    $.mockjax({
        url: '/fakeservice/1',
        type: 'POST',
        dataType: 'json',
        responseText: { 'name1': 'bar' }
    });

    testAjax().then(
        function (response) {
            deepEqual(response.name1, "bar", "no name1");
            start();
        },
        function (error) {
            ok(false, "got AJAX error");
            start();
        }
    );

});

参见 your adapted example on jsFiddle .

关于javascript - Mockjax 在同一个测试文件中两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14780749/

相关文章:

javascript - 为什么 JavaScript Promise Catch 没有被触发

javascript - SimpleModal:窗口调整大小会导致滚动条和模式内容的重绘

javascript - 如何在不使用 QUnit.push 的情况下为 QUnit 编写新的断言函数?

jquery - 我的 qUnit 测试套件需要许多不同的全页 DOM 示例

javascript - HTML 加载页面微调器不会褪色

javascript - 通过 JavaScript 设置的表单字段的值不会作为 $_POST 的一部分传递给 PHP 脚本

javascript - 如何对 AngularJS 服务/工厂进行单元测试

c# - 什么 block 没有被覆盖?

unit-testing - 使用 Karma 在移动设备上测试 Javascript

ember.js - 如何在 ember 中 stub 组件的操作?