2012-01-09 68 views
3

设置单元测试(特别是使用Jasmine)的最佳方式是使用多个版本的jQuery进行测试。显而易见的方法是让全局测试页面设置一系列iframe,但我想知道是否有方法在单个页面中实现它。作为一个天真的第一刺,我已经尝试了以下,未能运行任何测试。针对jQuery的多个版本运行jasmine测试

有没有人有任何想法如何修改,或更好的方法?

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(); 
+0

审议这个多了,我想我们需要的是异步运行茉莉测试套件的一些方法,所以我打算用waitsFor()方法 – wheresrhys 2012-01-09 10:54:37

+1

关于进一步反射实验,我想的iframe解决方案是最好的,因为它也允许对各种文档类型进行测试,但是如果任何人仍然觉得像上面提到的那样找到探针的解决方案,我仍然很想知道如果可能的话。 – wheresrhys 2012-01-09 11:35:16

回答

1

$版本jQuery库可能做的伎俩,我还没有尝试过自己尚未虽然:

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

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

+0

这看起来很整齐,但已经阅读了源代码我不相信它支持具有一些异步行为的测试 – wheresrhys 2012-01-23 11:06:21

+1

实际上,你可能是对的。将testready回调添加到testExecuter然后加载下一个jquery应该是相当简单的。可以使用“QUnit.done”事件。 – 2012-03-15 07:36:55