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;
                    });                
            }
        };
    }
      ])

    ;
});

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.