如何进行单元测试以查看角度是否未定义? [英] How can I do a unit test to see if angular is undefined?

查看:25
本文介绍了如何进行单元测试以查看角度是否未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单元测试中使用 angular version 1 和 jasmine.

I'm using angular version 1 with jasmine for my unit tests.

我想做的测试是:

例如,当我在 ie7 中加载 index.html 页面时,我加载了一个 html 横幅,上面写着下载最新的浏览器.

When I load the index.html page in ie7 for example I load in a html banner saying download latest browser.

我目前正在索引页上使用 JQuery 加载 html 模板.

I'm currently loading a html template using JQuery on the index page.

是否可以测试它,因为它在 angular 应用程序之外.

Is it possible to test this since its outside the angular app.

这是我在 index.html 页面上的当前代码:

This is my current code on my index.html page:

<!doctype html>
    <head>       
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>      

        <base href="/app/" />

         <!-- Is Angular supported -->  
        <script>
            if(window.angular === undefined)              
                 $(function(){$("#angularNotSupportedModal").load("/app/noAngularModal.html");});
        </script>   

    </head>
    <body ng-app="app" ng-cloak> 
        <div id="angularNotSupportedModal"></div>      

        <div ui-view></div>

        <!-- Application -->
        <script src="/scripts/app.js"></script>

        <!-- Config -->
        <script src="/scripts/config.js"></script>
    </body>
</html>

任何建议都会很棒.

谢谢

推荐答案

任务缩小到很好的旧 jQuery 测试,这是乏味但可能的.jQuery 是否可以被监视或应该完全被存根和模拟,这取决于代码.

The task narrows down to good old jQuery testing, which is tedious but possible. It depends on the code if jQuery can be just spied or should be totally stubbed and mocked.

被测试的脚本应该被隔离到单独的函数来正确测试.可能是这样的:

The tested script should be isolated to separate function to be tested properly. It may be something like this:

it('should call through $ chain', (done) => {
  var angular = window.angular;
  window.angular = undefined;

  spyOn(jQuery.prototype, 'ready').and.callThrough();
  spyOn(jQuery.prototype, 'load').and.callThrough();

  // make spy not break the chain
  var init = jQuery.prototype.init.bind(jQuery.prototype);
  spyOn(jQuery.prototype, 'init').and.callFake(init);

  window.runAngularNotSupportedFunction();

  expect(jQuery.prototype.ready).toHaveBeenCalledWith(jasmine.any(Function));
  expect(jQuery.prototype.init).toHaveBeenCalledWith('#angularNotSupportedModal', undefined);
  expect(jQuery.prototype.load).toHaveBeenCalledWith('/app/noAngularModal.html');

  window.angular = angular;
  jQuery.ready.promise().done(done)
})

这篇关于如何进行单元测试以查看角度是否未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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