0

I have an Angular JS directive which has a controller and that controller internally calls many services. How to test this using Karma.

Here is my code structure,

  1. In my directive I am displaying the data from service which internally gets data from controller. It is as below.

    <div class='tile'>

    <div class="count">{{tileRegistry.tileData.tileCount}}</div>

    </div>

Here tileRegistry is getting data from controller service as below

var commonController =  ['$scope','$filter','tileSvc','navSvc','appSetting',  function ($scope,$filter, tileSvc,navSvc,applSettig) {

 function commonController() {
    var tileType = $scope.tile.id;
    $scope.tileRegistry=tileSvc.getRegistry(tileType);
    $scope.limit=6;
 }
 commonController();
 $scope.link = function(){       
     var url = $scope.tile.uriLink;     
     applSetting.setpatientListActive(false);
     navSvc.navigateToSegment(url, {});
 };

}];
  1. tileSvc is the service called from controller like below
    angular module('app').service('tileSvc',['pblmSvc','procSvc','allgSvc',
        function(pblmSvc,procSvc,allgSvc){
        this.getRegistry = function(tileType){
            switch(tileType){
            case 'ALG': return allgSvc.registry; break;
            case 'PROB: return pblmSvc.registry; break;
            default: return null;
       }
    };
    }]);
    

please can anyone tell me how I can mock this controller and service in order to test my directive.

Thanks

1 Answer 1

0

pass mock json objects to $controller() and override necessary service calls:

beforeEach(function () {
    module('you_app_module');

    var $scope;
    inject(function ($injector, $controller, $rootScope, $filter) {
        $scope = $rootScope.$new();

        var tileSvcMock = {
            getRegistry: function (tileType) {
                return "some mock data";
            }
        };
        var navSvcMock = {
            navigateToSegment: function (param1, param2) {
                return "some mock data";
            }
        };
        var appSettingMock = {

        };
        $controller(commonController,
            {
                $scope: $scope,
                $filter: $filter,
                tileSvc: tileSvcMock,
                navSvc: navSvcMock,
                appSetting: appSettingMock
            });
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

After this how to validate the directive, will it automatically populate the data in my directive in the place of <div class="count">{{tileRegistry.tileData.tileCount}}</div> Please tell
sorry, never tested directive, usually directive is calling some controller or service method, so you should test that method, no directive. But! if you type in google "Angular directive testing", you will find that article angular-tips.com/blog/2014/06/… which tells, that passing simple html and scope to $compile('<some html>')(your_controller_scope) will call your directive. Find out more articles, you are not first person trying to test directive. Good luck!

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.