17

Hi I am building an app using AngularJS and I am stuck at unit test section. I know how to write unit testing for controllers and all but I don't know how to do it for routeProvider. I am using Jasmine for writing unit test.

My route provider will look like this;

    var app = angular.module('MyApp', ['ngResource'])

     app.config(function ($routeProvider) {
        $routeProvider
          .when('/', {
            templateUrl: 'app/views/main.html',
            controller: 'MainCtrl'
          })
          .when('/home/:PartyID', {
            templateUrl: 'app/views/home.html',
            controller: 'HomeCtrl'
          })
           .when('/edit/:PartyID', {
            templateUrl: 'app/views/update_profile.html',
            controller: 'EditCtrl'
          })
          .when('/route', {
            templateUrl: 'app/views/route.html',
            controller: 'RouteCtrl'
          })
          .when('/signup', {
            templateUrl: 'app/views/signup.html',
            controller: 'SignupCtrl'
          })
          .when('/login', {
            templateUrl: 'app/views/login.html',
            controller: 'LoginCtrl'
          })
          .otherwise({
            redirectTo: '/'
          });  
 });

How can I write unit test for this routeProvider using Jasmine?

Eduard Gamonal
  • 8,023
  • 5
  • 41
  • 46
BKM
  • 6,949
  • 7
  • 30
  • 45
  • 1
    I guess you don't want to write tests for routeProvider but to check your urls instead. This is in the tutorial http://docs.angularjs.org/tutorial/step_07 – Eduard Gamonal Jul 31 '13 at 07:05
  • So you are telling that I don't need to write unit test for routeProvider, rather write end2end test to check the url's. Right? – BKM Jul 31 '13 at 08:41
  • yeah, routeprovider is a service already tested by the angular guys. you just use an instance to modify the object that stores your urls. – Eduard Gamonal Jul 31 '13 at 09:46
  • 2
    Unit Testing routes is perfectly reasonable. The intent is not to test the (soon to be deprecated) $routeProvider but to test the mapping between url, template and controller. [Here](http://stackoverflow.com/a/17976106/449531) is an example. – zhon Sep 13 '13 at 06:02
  • 2
    _Unit test_ means testing some internal logic of a unit of your code. What you're trying to do is testing _configuration_. That is outside all of your "units" and should only be tested in end-to-end tests. – hon2a Dec 13 '14 at 18:34

2 Answers2

23

Yes you can, is the quick answer and below is a little piece of code that can be used and extended to test that routes take you to the places you'd expect.

describe('Testing routes', function() {
    beforeEach(module('windscreens'));

    var location, route, rootScope;

    beforeEach(inject(
        function( _$location_, _$route_, _$rootScope_ ) {
            location = _$location_;
            route = _$route_;
            rootScope = _$rootScope_;
    }));

     describe('Login route', function() {
        beforeEach(inject(
            function($httpBackend) {
                $httpBackend.expectGET('login')
                .respond(200);
            }));

        it('should load the login page on successful load of /login', function() {
            location.path('/login');
            rootScope.$digest();
            expect(route.current.controller).toBe('LoginCtrl')
        });
    });    
});
isherwood
  • 58,414
  • 16
  • 114
  • 157
Nick Lewis
  • 772
  • 1
  • 10
  • 20
15

Keep it simple

describe('Testing Routes', function () {

// load the controller's module
beforeEach(module('MyApp'));

it('should test routes',
inject(function ($route) {

  expect($route.routes['/'].controller).toBe('MainCtrl');
  expect($route.routes['/'].templateUrl).toEqual('app/views/main.html');

  expect($route.routes['/home/:PartyID'].controller).toBe('HomeCtrl');
  expect($route.routes['/home/:PartyID'].templateUrl).toEqual('app/views/home.html');

  expect($route.routes['/edit/:PartyID'].controller).toBe('EditCtrl');
  expect($route.routes['/edit/:PartyID'].templateUrl).toEqual('app/views/update_profile.html');

  expect($route.routes['/route'].controller).toBe('RouteCtrl');
  expect($route.routes['/route'].templateUrl).toEqual('app/views/route.html');

  expect($route.routes['/signup'].controller).toBe('SignupCtrl');
  expect($route.routes['/signup'].templateUrl).toEqual('app/views/signup.html');

  expect($route.routes['/streamconfigs/:id'].controller).toBe('LoginCtrl');
  expect($route.routes['/streamconfigs/:id'].templateUrl).toEqual('app/views/login.html');

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

});
Joey Gutierrez
  • 319
  • 3
  • 3