Javascript 如何使用jasmine编写jquery单元测试?

Javascript 如何使用jasmine编写jquery单元测试?,javascript,jquery,angularjs,unit-testing,jasmine,Javascript,Jquery,Angularjs,Unit Testing,Jasmine,我正在使用angular version1,需要在index.html页面上测试一些jquery 我已经将jquery添加到我的karma conf中,但是当我运行这个测试时,jquery是未定义的 我要测试的代码: <!-- Is Angular supported --> <script> if(window.angular === undefined)

我正在使用angular version1,需要在index.html页面上测试一些jquery

我已经将jquery添加到我的karma conf中,但是当我运行这个测试时,jquery是未定义的

我要测试的代码:

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

        <div id="angularNotSupportedModal"></div>      
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);
  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)
})

您应该在拆卸过程中重置
window.angular
,否则,如果测试失败,它将不会重置。如果您想单元测试前端代码,您必须开始使用OO编写解耦模块,以便将依赖项外部化。你所做的是正确的,但可能会容易得多。现在,你的问题是什么?有错误吗?当我运行上面的测试时,我似乎遇到了JQuery错误,知道吗?我遇到了一个错误,这行代码是:window.runAngularNotSupportedFunction