【问题标题】:Running jasmine tests against multiple versions of jQuery针对多个版本的 jQuery 运行 jasmine 测试
【发布时间】:2012-02-05 15:40:28
【问题描述】:

设置使用多个版本的 jQuery 进行测试的单元测试(尤其是使用 Jasmine)的最佳方法是什么。显而易见的方法是让全局测试页面简单地设置一系列 iframe,但我想知道是否有办法在单个页面中实现它。作为一个天真的第一次尝试,我尝试了以下方法,但无法运行任何测试。

有没有人知道如何修改它或采取更好的方法?

function testSuite() {
    describe("jquery.flickbook", function() {
          it("makes testing JavaScript awesome!", function() {
            expect(true).toBeTrue();
          });
    });
}

function testAllJquerys() {
    var jqueryInclude = document.getElementById("jquery"),
        head = document.getElementsByTagName("head")[0],
        versions = "1.6,1.7.1,1.5.1".split(",");


    function runSuite() {
        describe("jquery-" + version, testSuite);
        switchjQuery();
    }

    function switchjQuery() {
        version = versions.shift();
    //  jQuery = $ = null;
        jqueryInclude = document.createElement("script");
        jqueryInclude.type = "text/javascript";
        if (jqueryInclude.addEventListener){  
          jqueryInclude.addEventListener('load', runSuite, false);   
        } else if (jqueryInclude.attachEvent){  
          jqueryInclude.attachEvent('onload', runSuite);  
        }  
        head.appendChild(jqueryInclude);
        jqueryInclude.src = "../../jquery/jquery-" + version + ".js";
    }

    switchjQuery()
}

testAllJquerys();

【问题讨论】:

  • 考虑到这一点,我认为需要一些异步运行 jasmine 测试套件的方法,所以我将尝试使用 waitsFor() 方法
  • 经过进一步思考,我认为 iframe 解决方案是最好的,因为它还允许针对各种 doctype 进行测试,但如果有人仍然想找到上述问题的解决方案,我仍然很好奇知道是否可能

标签: javascript jquery unit-testing jasmine


【解决方案1】:

$versions jQuery 库可以解决问题,不过我自己还没有尝试过:

“在多个版本的 jQuery 中测试插件的优雅方式。 与 QUnit(可能还有其他测试框架)完美配合。”

https://github.com/protonet/jquery-versions

【讨论】:

  • 这看起来很简洁,但是阅读了源代码后我不相信它支持具有一些异步行为的测试
  • 实际上,您可能是对的。向 testExecuter 添加一个 testsready 回调然后在其中加载下一个 jquery 应该是相当简单的。可以为此使用“QUnit.done”事件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多