Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we write unit test for AngularJS routeProvider?

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?

like image 471
BKM Avatar asked Jul 31 '13 07:07

BKM


People also ask

Is AngularJS code unit testable?

AngularJS is written with testability in mind, but it still requires that you do the right thing. We tried to make the right thing easy, but if you ignore these guidelines you may end up with an untestable application.

What is unit testing in Angular JS?

Unit testing is an important step in the software development process which makes sure that every unit of code in all layers of the application is tested successfully. Angular. js is a widely used client-side MVC framework in modern applications.

Which one is the test entry file for the unit test?

The angular-cli configuration of karma uses the file “test. ts” as the entry point of the tests for the application.


1 Answers

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')
        });
    });    
});
like image 147
Nick Lewis Avatar answered Oct 05 '22 18:10

Nick Lewis