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

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

问题描述

我正在使用角度版本1和茉莉花进行单元测试。

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.

我正在加载一个html在索引页面上使用JQuery的模板。

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

是否有可能测试它,因为它在angular app之外。

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天全站免登陆