使用夹具进行QUnit奇怪行为,测试交替失败并通过 [英] QUnit strange behavior with fixture, tests alternately failing and passing

查看:88
本文介绍了使用夹具进行QUnit奇怪行为,测试交替失败并通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在QUnit中设置了以下内容:

I have the following set up in QUnit:

/* Dozen or so previous tests here */

test("Test some markup generation", function () {

    $('#qunit-fixture').plugin(); // jQuery plugin: Generates a table

    var rows = $('#qunit-fixture table tbody tr');
    count = rows.length;  // Count the rows
    console.log(count);

    equal(count, "96", "Expect the number of rows to be 96");
});

当它运行时,或当我刷新浏览器时,它交替地使这个测试显示count = 0,或者通过这个并失败所有以前的测试。测试之外没有定义全局变量。如果我手动将计数设置为96,一切都很顺利,或者如果我删除了这个测试,或者所有以前的测试,一切都会通过。我想知道是否有人遇到过这种行为?我之前使用过QUnit并且之前没有遇到过这种情况。

When it runs, or when I refresh the browser it alternately fails this test showing count = 0, or passes this and fails all the previous tests. There are no global variables defined outside the tests. If I set count to 96 by hand everything passes fine, or if I remove this test, or all the previous tests, everything also passes. I am wondering if anyone has run into this behavior? I've used QUnit quite a bit and have not encountered this before.

推荐答案

好的,我弄清楚了问题是什么与使用他们提供的夹具元素有关。 QUnit文档声明:

Ok, I figured out what the issue and it has to do with using their provided fixture element. The QUnit documentation states that:


#qunit-fixture元素可用于提供和操作测试标记,并在以后自动重置每个测试

The #qunit-fixture element can be used to provide and manipulate test markup, and will be automatically reset after each test

通过重置它们意味着它将被清空,没有你可能添加到它的任何其他属性重启。看看我的问题,你可以看到我正在将插件直接应用到灯具中,并且所有添加的属性都会在下一个测试中出现,导致这些问题。

By reset they mean it will just be "emptied", not have any additional properties that you may have added to it be reset. Looking at my questions you can see I was applying the plugin directly to the fixture and all of the added properties were hanging around for the next test causing these issues.

每个测试我现在在夹具中插入一个新元素并使用插件定位:

Before each test I now insert a new element into the fixture and target that with the plugin:

$('#qunit-fixture').append('<div id="target"></div>');
$('#target').plugin();

这个添加的元素在每次测试后都会被正确清除。虽然这看起来很明显,但我从文档中并不是很清楚。

This added element then get cleared correctly after each test. While this seems obvious now it was not immediately clear to me from the documentation.

更新:

更改已提交并于2012年2月14日提交至QUnit: https://github.com/jquery/ qunit / pull / 195

Change submitted and pulled into QUnit on 14/2/2012: https://github.com/jquery/qunit/pull/195

谢谢,Jörn

这篇关于使用夹具进行QUnit奇怪行为,测试交替失败并通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆