How do I inject $rootScope into an AngularJS unit test?

angularjs, dependency-injection, jasmine, javascript, unit-testing

Solution

...
var $rootScope;
beforeEach(inject(function(_$rootScope_) {
  $rootScope = _$rootScope_;
}));
...

Problem

Suppose I have a service that depends on a value in $rootScope, as with the following (trivial) service: ``` angular.module('myServices', []) .factory('rootValGetterService', function($rootScope) { return { getVal: function () { return $rootScope.specialValue; } }; }); ``` If I want to unit test this by putting a value in $rootScope, what is the best way to go about it?

Original source