Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何进行单元测试以确定角度是否未定义?_Javascript_Jquery_Angularjs_Unit Testing_Jasmine - Fatal编程技术网

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

Javascript 如何进行单元测试以确定角度是否未定义?,javascript,jquery,angularjs,unit-testing,jasmine,Javascript,Jquery,Angularjs,Unit Testing,Jasmine,我使用angular版本1和jasmine进行单元测试 我想做的测试是: 例如,当我在ie7中加载index.html页面时,我会加载一个html横幅,上面写着“下载最新浏览器” 我目前正在索引页面上使用JQuery加载一个html模板 有没有可能测试这一点,因为它在angular应用程序之外 这是我在我的index.html页面上的当前代码: <!doctype html> <head> <script src="https:

我使用angular版本1和jasmine进行单元测试

我想做的测试是:

例如,当我在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>

if(window.angular==未定义)
$(function(){$(“#angularNotSupportedModal”).load(“/app/noAngularModal.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)
})

这项任务的范围缩小到好的老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)
})

这是测试jQuery代码的方法。你还没有指定你做了什么以及为什么它不起作用。它找不到你使用的jQuery变量,我应该注入它吗?如果是,我该怎么做?是的,你应该在测试中加载jQuery和Angular。这是我在运行测试时遇到的git bash错误:PhantomJS 1.9.8(Windows 7 0.0.0)受支持的测试应通过jquery链调用FAILED TypeError:“undefined”不是对象(评估“jquery.prototype”),这是测试jquery代码的方法。你还没有指定你做了什么以及为什么它不起作用。它找不到你使用的jQuery变量,我应该注入它吗?如果是,我该怎么做?是的,你应该在测试中加载jQuery和Angular。这是我在运行测试时遇到的git bash错误:PhantomJS 1.9.8(Windows 7 0.0.0)是否支持通过jquery链调用的测试失败类型错误:“未定义”不是对象(正在评估“jquery.prototype”)