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

Joe*_*ann 3 unit-testing angularjs angularjs-directive karma-jasmine ngroute

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

运行我的测试套件时,在调用$ compile函数后出现错误:当它试图评估urlIsSameOrigin:

TypeError:'undefined'不是对象(评估'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));
    });
  });
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪

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 
Run Code Online (Sandbox Code Playgroud)

gka*_*pak 6

问题是该uiCoreTable指令通过一个返回元素template属性值的函数定义了它的template-URL .

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


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

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

如果您不想向服务器发出实际请求,则可以预先填充$templateCache一些HTML(在密钥下/path/to/template.html)或$httpBackend在请求目标时使用自定义响应训练模拟/path/to/template.html.