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?
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.
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.
The angular-cli configuration of karma uses the file “test. ts” as the entry point of the tests for the application.
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')
});
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With