给定一个附加到 $routeChangeSuccess 事件的处理程序来更新 $rootScope 上的 title 属性:
$rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
$rootScope.title = 'My title';
});
在单元测试中,我希望执行路由更改会更新标题属性:
it('should set a title on $routeChangeSuccess', function () {
$location.path('/contacts');
$rootScope.$apply();
expect($rootScope.title).toBe('My title');
});
但是, $routeChangeSuccess 处理程序中的代码永远不会在测试中执行。当在浏览器中运行应用程序时,它执行良好并更新标题。
如何在测试中触发 $routeChangeSuccess 事件?是否有更好的方法来测试附加到 $rootScope.$on('$routeChangeSuccess', ...) 或其他 $routeChange 事件的任意代码?
尝试自己发送事件。
$rootScope.$broadcast('$routeChangeSuccess', eventData);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)