简体   繁体   English

qunit + mockjax:何时应在异步测试中调用mockjaxClear?

[英]qunit + mockjax: When should I call mockjaxClear in async tests?

I'm testing my frontend code using qunit and mockjax . 我正在使用qunitmockjax测试我的前端代码。 The structure of AJAX tests in mockjax's own test code looks like this ( jsfiddle ): mockjax自己的测试代码中 ,AJAX测试的结构如下所示( jsfiddle ):

var testURL = "/test/data",
    testData = { a: 1, b: "c" };

asyncTest("AJAX response test", 1, function() {
    $.mockjax({
        url: testURL,
        responseText : JSON.stringify(testData)
    });

    $.ajax({
        url: testURL,
        dataType: "json",
        success: function(data) {
            deepEqual(data, testData, 'AJAX response is OK');
        },
        complete: function() {
            start();
        }
    });

    $.mockjaxClear();
});

According to the mockjax documentation : 根据mockjax文档

* $.mockjaxClear() 
    Removes all mockjax handlers.

What I don't understand is why mockjaxClear is called right after the $.ajax() call. 我不明白的是为什么在$.ajax()调用之后mockjaxClear调用mockjaxClear The problem is if it does some sort of cleanup, as the documentation says, this cleanup will run before the AJAX response arrives (pls. see the console of this jsfiddle ). 问题在于它是否进行某种清理,如文档所述,该清理将 AJAX响应到达之前运行(请参见此jsfiddle的控制台)。 It seems more logical for me to do the cleanup in the handler of the complete event. 对我来说,在complete事件的处理程序中进行清理似乎更合乎逻辑。 Can anyone explain me why it is better to call mockjaxClear after $.ajax() ? 谁能解释给我,为什么在$.ajax()之后再调用mockjaxClear更好?

If you take a look at the code, you will see that the cleanup does not affect already "running" calls. 如果看一下代码,您会发现清理不会影响已经“运行”的调用。 It just makes sure that any subsequent $.ajax() will call jQuery's original method and that other internal state (but again, not affecting already pending "requests") is cleaned. 它只是确保任何后续的$.ajax()都将调用jQuery的原始方法,并清除其他内部状态(但同样,不影响已经挂起的“请求”)。

This may help to ensure that the $.ajax() call under test test is only sent once (if more are sent they will fail, moreover the start() method will be called again, reporting the error to Qunit). 这可能有助于确保被测$.ajax()调用仅发送一次(如果发送了更多调用,它们将失败,此外,将再次调用start()方法,并将错误报告给Qunit)。

It could also be this way just to keep code clean (less stuff in the callback handlers). 也可能只是为了保持代码干净(回调处理程序中的内容更少)。

I don't think you should be running $.mockjaxClear anywhere in your tests actually. 我认为您实际上$.mockjaxClear在测试中的任何地方运行$.mockjaxClear QUnit provides lifecycle hooks for tests run within a module, the important one being teardown in this case. QUnit为模块内运行的测试提供了生命周期挂钩,在这种情况下,重要的是拆卸。

http://api.qunitjs.com/module/ http://api.qunitjs.com/module/

Using that, your code should be something like 使用它,您的代码应该类似于

module( "myapi", {
    teardown: function() {
        $.mockjaxClear();
    }
});

asyncTest( "sometest", function() {
    // test definition
});

If you wanted to, you could even move your mock setups into the setup lifecycle hook to make your actual test code more compact and focused just on the test itself, not setup/teardown. 如果愿意,您甚至可以将模拟设置移到setup生命周期挂钩中,以使您的实际测试代码更紧凑,并且仅关注测试本身,而不关注设置/拆卸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM