1

I have the following controller:

(function () {

    "use strict";

    angular.module('usp.configuration').controller('ConfigurationController', ConfigurationController);

    ConfigurationController.$inject = ['$scope', '$rootScope', '$routeParams', 'configurationService'];


    function ConfigurationController($scope, $rootScope, $routeParams, configurationService) {

        //Get Master Gas List
        configurationService.getMasterGasList().then(function (response) {
            $scope.masterGasList = response.data.data;
        });

        $scope.convertToInt = function (str) {
            if (!isNumberEmpty(str) && !isNaN(str)) {
                return parseInt(str, 10);
            }
            return "";
        }

        $scope.convertToString = function (num) {
            if (!isNumberEmpty(num) && !isNaN(num)) {
                return num + "";
            }
            return "";
        }


    }

}());

And below is the test case for the controller:

describe("test suite for Configuration test controller", function() {

    var scope = null;
    var configurationService;

    beforeEach(module("usp.configuration"));

    beforeEach(inject(function($rootScope, $controller, _configurationService_) {


         // Services
         // _'s are automatically unwrapped
        configurationService = _configurationService_;


        // Controller Setup
        scope = $rootScope.$new();
        $controller("ConfigurationController", {
            $scope: scope,
            configurationService : configurationService
        });
    }));

    it("should convert to int", function() {
        expect(scope.convertToInt("2")).toEqual(2);
    });

    it("should return empty string", function() {
        expect(scope.convertToInt("asd")).toEqual("");
    });
});

I don't want to call that service while I am running the test case.

I am new to unit testing, I don't know how can I do this.

Please help me to do this?

Anita Mehta
  • 625
  • 2
  • 13
  • 30

1 Answers1

1

You need to mock the dependencies with $provide

beforeEach(function () {
   configurationServiceMock = {
      getSomething: function () {
        return 'mockReturnValue';
      }
   };

   module(function ($provide) {
      $provide.value('configurationService', configurationServiceMock);
   });
});

see: Injecting a mock into an AngularJS service

Solution for your needs:

var configurationServiceMock = {
   getMasterGasList: function () {
      return { 
         then: function(callback) {}
      };
   }
};

beforeEach(inject(function ($rootScope, $controller) {
   scope = $rootScope.$new();
   controller = $controller('ConfigurationController', {
     '$scope': scope,
     'configurationService': configurationServiceMock
   });
}));
Community
  • 1
  • 1
trollr
  • 1,095
  • 12
  • 27
  • But I want to restrict to be executed, I am not getting how to do this, can you please help me to do? – Anita Mehta Apr 13 '16 at 08:27
  • I'll create a fiddle for you. Just gimme a sec – trollr Apr 13 '16 at 08:29
  • http://jsfiddle.net/trollr/pgkdquje/ here is a fiddle. I need to remove your isNumberEmpty function, because i don't know what you check there and $routeParams because i want it as easy as possible. However this fiddle is working and it should fit your needs – trollr Apr 13 '16 at 09:15
  • Still I am getting the same error and it is calling configurationService....I think this code has to be come after inject the controller or like that only – Anita Mehta Apr 13 '16 at 09:20
  • You see that the fiddle is working. I can't remote debug your code. Just think about and try to find your error – trollr Apr 13 '16 at 09:21
  • in my service code ConfigurationService.$inject = ['$http', 'DEVICE_MANAGEMENT_API_HOSTNAME']; it is like that and I am getting error:Error: [$injector:unpr] http://errors.angularjs.org/1.4.1/$injector/unpr?p0=DEVICE_MANAGEMENT_API_HOSTNAMEProvider – Anita Mehta Apr 13 '16 at 09:23
  • ah i see remove the line `configurationService = _configurationService_;` and change `inject(function($rootScope, $controller, _configurationService_)` to `inject(function($rootScope, $controller))` – trollr Apr 13 '16 at 09:25
  • One more clarification, if I multiple function inside the service, same way I have to mock or we have to any other method – Anita Mehta Apr 13 '16 at 10:16
  • Glad I could help you. I would suggest to use the same way otherwise you can use spies. However for spies you need to change your controller a little bit, and move the initial service call into a method. See: http://angular-tips.com/blog/2014/03/introduction-to-unit-test-spies/ – trollr Apr 13 '16 at 10:17