如何进行单元测试以查看角度是否未定义

How can I do a unit test to see if angular is undefined?

本文关键字:是否 未定义 何进行 单元测试      更新时间:2023-10-20

我在单元测试中使用了带有jasmine的angular版本1。

我想做的测试是:

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

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

有没有可能测试一下,因为它在angular应用程序之外。

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

<!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是否可以被监视,或者应该被完全清除和嘲笑。

测试的脚本应该被隔离,以分离要正确测试的函数。它可能是这样的:

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)
})