angularjs路由单元测试

all*_*kim 41 unit-testing jasmine angularjs angularjs-routing

正如我们在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'});
}]);
Run Code Online (Sandbox Code Playgroud)

建议使用e2e测试进行路由测试,

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

但是,我认为'$ routeProvider'配置是使用单个函数,函数($ routeProvider)完成的,我们应该能够在不涉及浏览器的情况下进行单元测试,因为我认为路由功能不需要浏览器DOM.

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

它是一个简单的IMO函数,它也应该在一个简单的测试,单元测试中进行测试.

我用谷歌搜索并搜索了这个$ routeProvider单元测试,但还没有运气.

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

zho*_*hon 63

为什么不断言路由对象配置正确?

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')
  });
});
Run Code Online (Sandbox Code Playgroud)

  • This looks like a pretty useless test to me. Not to say the style proposed by others is better. (13认同)
  • 这不会测试是否基于状态路线可用/ callabe.例如,这是如何涵盖"我没有登录并尝试访问受限资源"的情况? (2认同)

小智 45

我认为你应该能够像这样测试$ routeProvider:

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'});
}]);


it('should test routeProvider', function() {
  module('phonecat');

  inject(function($route, $location, $rootScope) {

    expect($route.current).toBeUndefined();
    $location.path('/phones/1');
    $rootScope.$digest();

    expect($route.current.templateUrl).toBe('partials/phone-detail.html');
    expect($route.current.controller).toBe(PhoneDetailCtrl);

    $location.path('/otherwise');
    $rootScope.$digest();

    expect($location.path()).toBe('/phones/');
    expect($route.current.templateUrl).toEqual('partials/phone-list.html');
    expect($route.current.controller).toBe(PhoneListCtrl);

  });
}); 
Run Code Online (Sandbox Code Playgroud)

  • 只需要使用正确的expectGET添加$ httpBackend.这是我的例子http://plnkr.co/edit/j1o0iu?p=preview (5认同)
  • 太棒了.`$ rootScope.$ digest()`正是我所需要的. (2认同)

pan*_*say 5

结合前面两个答案,如果你想把路由器测试成一个成功路由的黑匣子(不是控制器自己配置),无论路由是什么:

// assuming the usual inject beforeEach for $route etc.
var expected = {};
it('should call the right controller for /phones route', function () { 
    expected.controller = $route.routes['/phones'].controller;
    $location.path('/phones');
    $rootScope.$digest();
    expect($route.current.controller).toBe(expected.controller);
});

it('should redirect to redirectUrl from any other route', function () {
    expected.path = $route.routes[null].redirectTo;
    $location.path('/wherever-wrong');
    $rootScope.$digest();
    expect($location.path()).toBe(expected.path);
});
Run Code Online (Sandbox Code Playgroud)