angularjs - 单元测试 AngularJS 指令时,urlIsSameOrigin 函数失败

标签 angularjs unit-testing angularjs-directive karma-jasmine ngroute

这是我对 AngularJS 指令的第一次测试,我可能遗漏了一些非常基本的东西。

当运行我的测试套件时,我在 $compile 函数被调用后得到一个错误:当它试图评估是否 urlIsSameOrigin :

TypeError: 'undefined' is not an object (evaluating 'parsed.protocol')

tableDirective.js 指令

https://gist.github.com/jdreimann/a5076363f1d8f3a605a0

tableDirective.js 测试

'use strict';

describe('Directive: TableDirective', function() {

  var $rootScope,
  $compile,
  elmBody;

  beforeEach(module('wdUiCoreApp'));

  beforeEach(inject(function (_$compile_ , _$rootScope_) {
    $compile = _$compile_;
    $rootScope = _$rootScope_;
    elmBody = angular.element(
      '<div ui-core-table><div class="panel__header"></div></div>'
    );

    $compile(elmBody)($rootScope);
    scope.$digest();
  }));    

  describe('elmBody', function () {
    it('should be an object', function() {
      expect(elmBody).toEqual(jasmine.any(Object));
    });
  });

堆栈跟踪

TypeError: Cannot read property 'protocol' of undefined
    at urlIsSameOrigin (http://localhost:8082/base/app/bower_components/angular/angular.js:13820:17)
    at $http (http://localhost:8082/base/app/bower_components/angular/angular.js:7629:23)
    at Function.$http.(anonymous function) (http://localhost:8082/base/app/bower_components/angular/angular.js:7847:18)
    at compileTemplateUrl (http://localhost:8082/base/app/bower_components/angular/angular.js:6463:13)
    at applyDirectivesToNode (http://localhost:8082/base/app/bower_components/angular/angular.js:6066:24)
    at compileNodes (http://localhost:8082/base/app/bower_components/angular/angular.js:5669:15)
    at compile (http://localhost:8082/base/app/bower_components/angular/angular.js:5602:15)
    at null.<anonymous> (http://localhost:8082/base/test/spec/directives/tableDirective.js:18:5)
    at Object.invoke (http://localhost:8082/base/app/bower_components/angular/angular.js:3762:17)
    at workFn (http://localhost:8082/base/app/bower_components/angular-mocks/angular-mocks.js:2144:20)
Error: Declaration Location
    at window.inject.angular.mock.inject (http://localhost:8082/base/app/bower_components/angular-mocks/angular-mocks.js:2129:25)
    at null.<anonymous> (http://localhost:8082/base/test/spec/directives/tableDirective.js:11:14)
    at jasmine.Env.describe_ (http://localhost:8082/base/node_modules/karma-jasmine/lib/jasmine.js:884:21)
    at jasmine.Env.describe (http://localhost:8082/base/node_modules/karma-jasmine/lib/jasmine.js:869:15)
    at describe (http://localhost:8082/base/node_modules/karma-jasmine/lib/jasmine.js:629:27)
    at http://localhost:8082/base/test/spec/directives/tableDirective.js:3:1 

最佳答案

问题是 uiCoreTable 指令通过返回元素的 template 属性值的函数定义了它的模板 URL。

由于您的测试代码未指定 template 属性,因此模板 URL 未定义,当 Angular 尝试从服务器请求模板 URL 时会抛出错误。
更具体地说,$http 服务的一项安全检查会引发异常:检查 URL 的 protocol 的测试试图访问 parsed.protocol,但 parsed 未定义(因为 templateUrl 未定义)。


如果现在还不明显,为了使错误消失,您需要在 template 属性中指定模板 URL。例如:

elmBody = angular.element('<div ui-core-table template="/path/to/template.html">...</div>');

<子> 如果您不想向服务器发出实际请求,您可以使用一些 HTML 预填充 $templateCache(在键 /path/to/template.html) 或者在请求指向 /path/to/template.html 时使用自定义响应训练模拟的 $httpBackend

关于angularjs - 单元测试 AngularJS 指令时,urlIsSameOrigin 函数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24509699/

相关文章:

angularjs - Angular UI Bootstrap 崩溃 - 怎么了?

javascript - 迷你图的数据帧不平衡

javascript - 使用 $watch 更新数据,ng-repeat 不反射(reflect)变化

javascript - Angular 应用程序以及国际化和本地化

javascript - 在 Angularjs 中访问模板内的属性值

windows - x86 LargeAddressAware 兼容性的单元测试

unit-testing - 主要与外部资源交互的单元测试程序

unit-testing - 在 Grails 中,如何在自动化测试中测试 @Secured 注释?

AngularJS 在指令内以编程方式触发 anchor 标记上的单击事件

javascript - Angular 指令 - 函数执行的正确顺序