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

时间:2016-04-01 10:03:05

标签: javascript jquery angularjs unit-testing jasmine

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

我想做的测试是:

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

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

是否可以测试这个,因为它在角度app之外。

这是我在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>

任何建议都会很棒。

由于

1 个答案:

答案 0 :(得分:0)

任务缩小到良好的旧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)
})