How to test run block of a module

angularjs

Solution

You can get the reference to the run block via the _runBlocks property of the module.

The file with your module code:

angular.module('xmpl', []).
  run(function() {
    // some code here
  });

The test suite:

var myModule = angular.module('xmpl'),
    runBlock = myModule._runBlocks[0];
// Now can test the runBlock function according to your scenario

Problem

I want my AngularJS app to make a http request to retrieve user information from the server or redirect to login screen. I implemented this in a run block of my app's main module. But how do I test the code in a run block? Or should I move this initialization code in a controller to make it testable? I'm writing my tests with Karma and Jasmine. Any help would be appreciated!

Original source