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

angularjs路由单元测试

发布时间:2020-12-17 08:50:36 所属栏目:安全 来源:网络整理
导读:正如我们在 http://docs.angularjs.org/tutorial/step_07看到的, angular.module('phonecat',[]). config(['$routeProvider',function($routeProvider) { $routeProvider. when('/phones',{templateUrl: 'partials/phone-list.html',controller: PhoneListC
正如我们在 http://docs.angularjs.org/tutorial/step_07看到的,
angular.module('phonecat',[]).
  config(['$routeProvider',function($routeProvider) {
  $routeProvider.
      when('/phones',{templateUrl: 'partials/phone-list.html',controller: PhoneListCtrl}).
      when('/phones/:phoneId',{templateUrl: 'partials/phone-detail.html',controller: PhoneDetailCtrl}).
      otherwise({redirectTo: '/phones'});
}]);

路由测试建议用e2e测试,

it('should redirect index.html to index.html#/phones',function() {
    browser().navigateTo('../../app/index.html');
    expect(browser().location().url()).toBe('/phones');
  });

然而,我认为’$ routeProvider’配置是完成与一个单一的函数,函数($ routeProvider),我们应该能够单位测试而不涉及浏览器,因为我认为路由功能不需要浏览器DOM。

例如,
当url是/ foo时,templateUrl必须是/partials/foo.html,而controller是FooCtrl
当url是/ bar时,templateUrl必须是/partials/bar.html,控制器是BarCtrl

它是一个简单的功能IMO,它也应该在一个简单的测试,一个单元测试。

我googled并搜索这个$ routeProvider单元测试,但没有运气。

我想我可以从这里借一些代码,但还不能做到,https://github.com/angular/angular.js/blob/master/test/ng/routeSpec.js。

为什么不只是断言路由对象配置正确?
it('should map routes to controllers',function() {
  module('phonecat');

  inject(function($route) {

    expect($route.routes['/phones'].controller).toBe('PhoneListCtrl');
    expect($route.routes['/phones'].templateUrl).
      toEqual('partials/phone-list.html');

    expect($route.routes['/phones/:phoneId'].templateUrl).
      toEqual('partials/phone-detail.html');
    expect($route.routes['/phones/:phoneId'].controller).
      toEqual('PhoneDetailCtrl');

    // otherwise redirect to
    expect($route.routes[null].redirectTo).toEqual('/phones')
  });
});

(编辑:李大同)

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

    推荐文章
      热点阅读