Angularjs unit test a controller dependent on route resolve

angularjs, jasmine, karma-runner, sinon, unit-testing

Solution

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
}

Problem

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: [...] } } [...] } [...] ```

Original source