javascript - Jasmine 与 jQuery Mobile 的兼容性

标签 javascript unit-testing jquery-mobile jasmine

我正在实现一些 Jasmine tests对于 jQuery mobile我正在处理的应用程序遇到了一个错误,我设法追踪到添加 jQuery 移动库,错误如下:

Jasmine.js:1769 TypeError: Cannot read property 'abort' of undefined.

只要删除 jQM 依赖项,错误就会消失。

这是我的代码:

<!DOCTYPE html>
<html>
<head>
    <title>HTML5/Common/Tests</title>
    <!-- LOAD STYLES FIRST -->
    <link type="text/css" rel="stylesheet" href="libs/jasmine.css" media="screen">
    <link type="text/css" rel="stylesheet" href="../../Common/libs/jquery.mobile-1.0.1.css" />
    <!-- LOAD JASMINE LIBRARIES -->
    <script type="text/javascript" src="libs/jasmine.js"></script>
    <script type="text/javascript" src="libs/jasmine-html.js"></script>
    <!-- LOAD DEPENDENCIES -->
    <script type="text/javascript" src="../../Common/libs/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="../../Common/libs/jquery.mobile-1.0.1.min.js"></script>
    <!-- LOAD CODE TO TEST -->
    <script type="text/javascript" src="../../Common/libs/myLib.js"></script>
    <!-- LOAD ACTUAL TESTS -->
    <script type="text/javascript">
       describe("Suite 1", function() {
            it("Should be that 1 equals 0", function() {
                  expect(0).toEqual(1);
            });
       });
    </script>
</head>
<body>
    <script type="text/javascript">
     jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
     jasmine.getEnv().execute();
    </script>
</body>
</html>

我更愿意为这个应用程序使用 Jasmine 而不是 qUnit因为我认为它更灵活,更容易在 CI 中实现并向 BA 和 PM 解释。但是在解决这个问题几个小时并在谷歌上进行了一些徒劳的搜索之后,我仍然无法解决它,所以我开始考虑继续前进。

不过,在我这样做之前,有没有人遇到过同样的问题并找到了解决方案?

感谢和问候。

3 月 20 日更新:

票在github Jasmine项目中:

https://github.com/pivotal/jasmine/issues/204

最佳答案

我能够使用 jasmine-jquery 对 jquery 移动脚本进行 jasmine 测试.

诀窍是您需要在 DOMready 上禁用 jquery 移动增强,因为它会尝试增强 jasmine runner 的 html。 您可以使用插入 html 运行程序文件头部的脚本来完成此操作:

<script type="text/javascript">
  $(document).bind("mobileinit", function(){
    $.mobile.autoInitializePage = false;
  });
</script>

然后你需要在从你的fixture插入的html上触发jquery mobile的增强(jasmine-jquery functionnality):

describe('my test', function(){
  beforeEach(function() {
    loadFixtures("fixtures/MYFixture.html");
    $("#jasmine-fixtures").attr("data-role","page").trigger("create");
  }
  it( ...
});

关于javascript - Jasmine 与 jQuery Mobile 的兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9784504/

相关文章:

javascript - 'this' 在生命周期方法中如何工作?

python - 如果测试通过,我怎样才能包括额外的测试?

jquery - 如何在自动完成中获取所选项目的值

javascript - 更改 jQuery 移动应用程序中的 AddThis 共享 URL

javascript - 创建一个包含字符串属性的对象文字

javascript - Android WebView : parallel Loading, 布局和 Javascript

javascript - 滚动到新生成的 div 的顶部

unit-testing - 使用 Haskell Stack 运行 detailed-0.9 测试套件时为 "module cannot be found locally"

javascript - 使用 sinon spies 验证函数调用并检查参数

css - JQuery Mobile 将 2 个元素的宽度设置为 80% 和 20%