Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs unit test a controller dependent on route resolve

I'm trying to unit test my controller, but it is dependent on some data that originates from when the route gets resolved. I just can't figure out how to test that (or not fail the test because it can't find "locals")

The controller:

myMainModule.controller('theController', function theController($scope, $route) {
    [...]
    $scope.variable = $route.current.locals.data;
    [...]
}

The route configuration:

[...].config(function ($routeProvider, $locationProvider) {
    $routeProvider.when([...], {
        templateUrl: [...],
        controller: 'theController',
        resolve: {
            data: [...]
        }
    }
    [...]
} [...]
like image 576
LostBalloon Avatar asked Mar 21 '14 21:03

LostBalloon


1 Answers

In your unit test, inject a fake $route instead of the real one:

var fakeRoute = {
    current: { 
        locals: {
            data: 'someFakeData'
        }
    }
}
$scontroller('theController', {
    $scope: $scope,
    $route: fakeRoute
}
like image 191
JB Nizet Avatar answered Sep 28 '22 07:09

JB Nizet