0

My app.run method as shown below.

 app.run(['urlconfigservice',
        function (urlconfigservice) {            
            urlconfigservice.loadurlconfiguration();
        }
      ]);

My urlconfigservice as shown below: Which reads urlconfig.json file and store result in urlconfiguration.

define([
    'angular'
], function (angular) {
    'use strict';
    return angular.module('urlconfigservice', [])
      .factory('urlconfigservice', [ //
            '$rootScope', '$http',
    function ($rootScope, $http) {        
        return {            
            loadurlconfiguration: function () {                
                    $http.get('urlconfig.json').success(function (data) {                        
                        $rootScope.urlconfiguration = data;
                    });                
            }
        };
    }
      ])

    ;
});
paulroho
  • 1,234
  • 1
  • 11
  • 27

1 Answers1

0

As stated by the documentation of angular, is the run block of a module hard to test and preferable not done. Like you do it should only use other services where the logic resides.

AngularJS documentation:

Run blocks are the closest thing in Angular to the main method. A run block is the code which needs to run to kickstart the application. It is executed after all of the services have been configured and the injector has been created. Run blocks typically contain code which is hard to unit-test, and for this reason should be declared in isolated modules, so that they can be ignored in the unit-tests.

You can however test that the factory is called like it done describes in this other post about testing a run block

Community
  • 1
  • 1
Ricconnect
  • 1,089
  • 5
  • 25
  • Thank u Ricconnect. But may i know , how can mock the run method. Because i am writing unit test of other controllers. it is showing Unexpected request : Get urlconfig.json. No more request expected. – Vasanth kumar R.L. Feb 17 '15 at 09:21
  • Could you update your question with code of the unit test you are having this problem with. With loadConfiguration you will send a async request of getting the configuration. It is higly possible that the configuration.json is load loaded yet when your unit test is run. – Ricconnect Feb 17 '15 at 09:37