当前位置: 首页 > 面试题库 >

angularjs路由单元测试

顾曾笑
2023-03-14
问题内容

正如我们在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’配置是通过单个函数function($
routeProvider)完成的,我们应该能够在不涉及浏览器的情况下进行单元测试,因为我认为路由功能不需要浏览器DOM。

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

这是IMO的简单功能,还应该在简单测试(单元测试)中进行测试。

我用Google搜索了此$ 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')
  });
});


 类似资料:
  • 主要内容:AngularJS 实例,路由设置对象,AngularJS 实例本章节我们将为大家介绍 AngularJS 路由。 AngularJS 路由允许我们通过不同的 URL 访问不同的内容。 通过 AngularJS 可以实现多视图的单页 Web 应用(single page web application,SPA)。 通常我们的 URL 形式为 http://xnip.cn/first/page,但在单页 Web 应用中 AngularJS 通过 #! + 标记

  • 问题内容: 我正在尝试测试一些视图,这些视图用于链接到应用程序中的其他状态。在我的测试中,我触发了对此元素的点击,如下所示: 如果状态切换为,该如何测试?在我的控制器中像这样使用时,这很容易: 但是当我使用时,我不知道要监视什么对象。如何验证我的应用程序处于正确状态? 问题答案: 我自己找到的。在查看了角度ui路由器源代码后,我在指令中找到了以下行: 当元素收到点击时,会包装在回调中。因此,在测试

  • 本文向大家介绍AngularJS 单元测试服务,包括了AngularJS 单元测试服务的使用技巧和注意事项,需要的朋友参考一下 示例 服务编号 考试 跑!

  • 问题内容: 我有一个控制器,它从中获取值并将其发送到其他状态: 我不确定如何对该测试方法进行单元测试。我该如何验证go方法已被调用或对Karma / AngularJS进行某种处理? 问题答案: 这两种声音听起来都可以用Jasmine间谍来完成。 这里有一个很好的间谍的cheatsheet http://tobyho.com/2011/12/15/jasmine-spy- cheatsheet/

  • 问题内容: 这是一个具有提交功能的控制器: 这是我的考验 我得到的错误是: views / appBar.html是我的templateUrl: 因此,以某种方式ui-router使我的$ httpBackend指向此位置而不是我的提交功能。使用$ httpBackend进行的所有测试都存在相同的问题。 有什么解决办法吗? 问题答案: 采取这个要点 https://gist.github.com/

  • 本文向大家介绍AngularJS 单元测试过滤器,包括了AngularJS 单元测试过滤器的使用技巧和注意事项,需要的朋友参考一下 示例 过滤器代码: 考试: 跑! 备注:在inject测试的调用中,您的过滤器需要使用其名称+ Filter来指定。原因是,每当您为模块注册过滤器时,Angular都会Filter在其名称后面附加一个注册它。