加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何在角度单元测试中模拟/触发$routeChangeSucces

发布时间:2020-12-17 07:46:25 所属栏目:安全 来源:网络整理
导读:给定一个附加到$routeChangeSuccess事件的处理程序来更新$rootScope上的title属性: $rootScope.$on('$routeChangeSuccess',function (event,current,previous) { $rootScope.title = 'My title';}); 在单元测试中,我希望执行路由更改会更新标题属性: it('s



给定一个附加到$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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读