Javascript 如何在定向链接功能中单元测试Angular routeChangeSuccess?

Javascript 如何在定向链接功能中单元测试Angular routeChangeSuccess?,javascript,angularjs,unit-testing,jasmine,Javascript,Angularjs,Unit Testing,Jasmine,Karma不断抛出TypeError:如果在链接函数中有以下指令,则无法读取未定义的的属性“originalPath”: angular.module('myApp').directive('sidebar', ['$route', function ($route) { return { restrict: 'E', templateUrl: 'views/sidebar.html', scope: { activeNav: '@' },

Karma不断抛出
TypeError:如果在
链接
函数中有以下指令,则无法读取未定义的
的属性“originalPath”:

angular.module('myApp').directive('sidebar', ['$route', function ($route)
{
  return {
    restrict: 'E',
    templateUrl: 'views/sidebar.html',
    scope: {
      activeNav: '@'
    },
    link: function (scope, element, attrs) {
      scope.$on('$routeChangeSuccess', function (event, curr, prev) {
        scope.activeNav = curr.$$route.originalPath || '/about';
      });
    }
}
和单元测试:

describe('sidebar directive', function () {
  var $compile,
    $rootScope,
    scope,
    element;

  beforeEach(module('MyApp'));
  beforeEach(module('my.templates')); // ng-html2js karma template loader

  beforeEach(inject(function(_$compile_, _$rootScope_){
    $compile = _$compile_;
    $rootScope = _$rootScope_;
    scope = $rootScope.$new();
  }));


  it('defaults to /about route', function() {
    element = $compile("<sidebar></sidebar>")(scope);
    var linkScope = element.children().scope();

    $rootScope.$broadcast('$routeChangeSuccess');
    $rootScope.$digest();
    expect(linkScope.activeNav).toBe('/about');
  });
});
description('sidebar指令',函数(){
var$compile,
$rootScope,
范围
元素;
在每个模块之前(模块(‘MyApp’);
beforeach(模块('my.templates');//ng-html2js karma模板加载器
beforeach(注入函数($compile,$rootScope){
$compile=\$compile;
$rootScope=\u$rootScope;
scope=$rootScope.$new();
}));
它('默认为/关于路由',函数(){
元素=$compile(“”)(范围);
var linkScope=element.children().scope();
$rootScope.$broadcast(“$routeChangeSuccess”);
$rootScope.$digest();
expect(linkScope.activeNav).toBe('/about');
});
});

当从链接中记录
curr
路由时,我得到
Object{params:Object{},pathParams:Object{},locals:Object{}
。我试着通过一条模拟路线发送广播消息,但没有任何改变。如何获得要传递到指令中的预期(默认)路由?这是跟踪链路内路由更改的正确方法吗?我猜我对Jasmine和单元测试还不熟悉。

我最终找到了使用
$route.reload()触发路由更改的方法。

从中可以看出:

导致$route服务重新加载当前路由,即使$location未更改。因此,ngView创建新的作用域并重新实例化控制器

因此,将其添加到我的测试中“重新启动”路由,强制routeChangeSuccess发出,并在消化后传递到
链接中。以下是等级库文件中的更新块,添加了$location更改以测试其他管线:

describe('sidebar directive', function () {
  var $compile,
    $rootScope,
    $route,
    $location,
    scope,
    element;

  beforeEach(module('MyApp'));
  beforeEach(module('my.templates')); // ng-html2js karma template loader

  beforeEach(inject(function(_$compile_, _$rootScope_, _$route_, _$location_){
    $compile = _$compile_;
    $rootScope = _$rootScope_;
    $location = _$location_;
    $route = _$route_;
    scope = $rootScope.$new();
    element = $compile("<sidebar></sidebar>")(scope);
  }));


  it('defaults to /about route', function() {
    $route.reload();
    $rootScope.$digest();
    var linkScope = element.children().scope(); // Get directive's isolated scope
    expect(linkScope.activeNav).toBe('/about');
  });

  it('sets activeNave to correct route on change', function() {
    $location.path('/foo');
    $route.reload();
    $rootScope.$digest();
    var linkScope = element.children().scope();
    expect(linkScope.activeNav).toBe('/foo');
  });
});
description('sidebar指令',函数(){
var$compile,
$rootScope,
$route,
$location,
范围
元素;
在每个模块之前(模块(‘MyApp’);
beforeach(模块('my.templates');//ng-html2js karma模板加载器
beforeach(注入函数($compile、$rootScope、$route、$location){
$compile=\$compile;
$rootScope=\u$rootScope;
$location=$location;
$route=$route;
scope=$rootScope.$new();
元素=$compile(“”)(范围);
}));
它('默认为/关于路由',函数(){
$route.reload();
$rootScope.$digest();
var linkScope=element.children().scope();//Get指令的隔离作用域
expect(linkScope.activeNav).toBe('/about');
});
它('将activeNave设置为在更改时更正路由',函数(){
$location.path('/foo');
$route.reload();
$rootScope.$digest();
var linkScope=element.children().scope();
expect(linkScope.activeNav).toBe('/foo');
});
});