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

单元测试AngularJS指令时,urlIsSameOrigin函数失败,angularjs,unit-testing,angularjs-directive,karma-jasmine,ngroute,Angularjs,Unit Testing,Angularjs Directive,Karma Jasmine,Ngroute,这是我对AngularJS指令的第一次测试,可能我遗漏了一些非常基本的东西 在运行我的测试套件时,调用$compile函数后,我收到一个错误:当它试图评估 TypeError:“undefined”不是对象(正在计算“parsed.protocol”) tableDirective.js指令 TypeError: Cannot read property 'protocol' of undefined at urlIsSameOrigin (http://localhost:8082/b

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

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

TypeError:“undefined”不是对象(正在计算“parsed.protocol”)

tableDirective.js指令

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 

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));
    });
  });
“严格使用”;
description('Directive:TableDirective',function(){
var$rootScope,
$compile,
埃尔姆博迪;
之前(模块('wdUiCoreApp'));
beforeach(注入函数($compile,$rootScope){
$compile=\$compile;
$rootScope=\u$rootScope;
elmBody=角度元素(
''
);
$compile(elmBody)($rootScope);
范围。$digest();
}));    
描述('elmBody',函数(){
它('应该是一个对象',函数(){
expect(elmBody).toEqual(jasmine.any(Object));
});
});
Stacktrace

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 
TypeError:无法读取未定义的属性“protocol”
原籍(http://localhost:8082/base/app/bower_components/angular/angular.js:13820:17)
售价$http(http://localhost:8082/base/app/bower_components/angular/angular.js:7629:23)
at函数。$http。(匿名函数)(http://localhost:8082/base/app/bower_components/angular/angular.js:7847:18)
在compileTemplateUrl(http://localhost:8082/base/app/bower_components/angular/angular.js:6463:13)
在applyDirectivesToNode(http://localhost:8082/base/app/bower_components/angular/angular.js:6066:24)
在编译节点(http://localhost:8082/base/app/bower_components/angular/angular.js:5669:15)
编译时(http://localhost:8082/base/app/bower_components/angular/angular.js:5602:15)
在空(http://localhost:8082/base/test/spec/directives/tableDirective.js:18:5)
在Object.invoke(http://localhost:8082/base/app/bower_components/angular/angular.js:3762:17)
在工作fn(http://localhost:8082/base/app/bower_components/angular-模拟/角度模拟。js:2144:20)
错误:声明位置
在window.inject.angular.mock.inject(http://localhost:8082/base/app/bower_components/angular-模拟/角度模拟。js:2129:25)
在空(http://localhost:8082/base/test/spec/directives/tableDirective.js:11:14)
在jasmine.Env.descripe\u(http://localhost:8082/base/node_modules/karma-jasmine/lib/jasmine.js:884:21)
在jasmine.Env(http://localhost:8082/base/node_modules/karma-jasmine/lib/jasmine.js:869:15)
至少描述(http://localhost:8082/base/node_modules/karma-jasmine/lib/jasmine.js:629:27)
在http://localhost:8082/base/test/spec/directives/tableDirective.js:3:1 

问题在于,
uiCoreTable
指令通过返回元素的
template
属性值的函数来定义其模板URL

由于测试代码未指定
模板
属性,因此模板URL未定义,并且在尝试从服务器请求模板URL时引发错误。
更具体地说,
$http
服务的一个安全检查引发了一个异常:检查URL的
协议的测试试图访问
已解析的协议,但
已解析的协议
未定义(因为
模板URL
未定义)


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

elmBody = angular.element('<div ui-core-table template="/path/to/template.html">...</div>');
elmBody=angular.element(“…”);
如果您不想向服务器发出实际请求,可以使用一些HTML(在键
/path/to/template.HTML
下)预先填充
$templateCache
),或者在请求指向
/path/to/template.HTML
时使用自定义响应训练模拟的
$httpBackend

那是什么样子的
uiCoreTable
吗?@ExpertSystem我已经把它添加到了问题中。