GiggleGirl
GiggleGirl

Reputation: 77

Unable to test a controller for service call

I am trying to write a code to test the service call which is done in my controller . I am trying to unit test that particular function in controller which is doing the service call and bringing the data . Currently i am trying with local json , but it will actually do a service call .

I got to know that first i have create a spy object but i am getting the error as "TypeError: jasmine.CreateSpyObj is not a function" , i am new to unit testing . I am unable to create a spyobject so unable to proceed further .Pasting my code , request you to help me please.

Also i am not sure what exactly i have to do once i successfully create spyObject , i actually want to test if my service is getting hit fine and i am getting response from service.

Please help me i am stryggling in this from many days now.

Service code :

//app is the module name
app.factory('appServices', ['$rootScope', '$http', function($rootScope, 
$http) {
var appServices = {};
appServices.getData = function(){
      return $http.get(scripts/services/data/unitTesting.json'); 
 };

unitTesting.json code :

{
"name":"unit testing",
"countryCode": "EG",
"countryName": "Egypt",
"region": "Africa"
}

Controller Code :

getData: function(){
        appServices.getData().then(function(response) {
            if (response && response.data) {
                $scope.testVariable= response.data.name;
            }
        });
    },

Unit Test Code :

  describe('myCtrl', function() {
      beforeEach(module('app'));

      var $controller;

      beforeEach(inject(function(_$controller_){
         $controller = _$controller_;
  }));

 describe('service call test', function() {
 var $http,$httpBackend,appServices,
 myService,$q,$rootScope,controller;

 var mockItem = 
       {
       "name":"unit testing",
       "countryCode": "EG",
       "countryName": "Egypt",
       "region": "Africa"
       }
 beforeEach(inject(function(_$http_,_$httpBackend_,appServices,_$q_, 
 _$rootScope_) {
   $http = _$http_;
   $httpBackend = _$httpBackend_; 
   appServices = appServices; 
   $rootScope = _$rootScope_;
   $q =_$q_;
   jasmine.spyOn(appServices, 'getData').and.returnValue($q.when(mockItem));
   controller = $controller('myCtrl', { $scope: $scope });
 }));
  it('Service call test ', function() {
  controller = $controller('myCtrl', { $scope: $rootScope.new() });
  controller.getData();
  expect(appServices.getData).toHaveBeenCalled();
 }); 
 });
 });

ERROR :

 TypeError: jasmine.spyOn is not a function

Upvotes: 1

Views: 100

Answers (1)

mindparse
mindparse

Reputation: 7265

Use the spyOn function:

jasmine.spyOn(appServices, 'getData');

You can then check for calls in your tests like:

expect(appServices.getData).toHaveBeenCalled();

Looking at how you have written your spec, can I suggest some other changes to help you get it to run:

var $rootScope,
    controller,
    $q;

beforeEach(inject(function(_$http_,_$httpBackend_,appServices,_$q_, _$rootScope_){
     $http = _$http_;
     $httpBackend = _$httpBackend_; 
     appServices= appServices;
     $rootScope = _$rootScope_;
     $q = _$q_;
     jasmine.spyOn(appServices, 'getData').and.returnValue($q.when(mockItem));
     controller = $controller('myCtrl', { $scope: $scope });
}));
describe('when fetching data in the controller', function(){
    it('should delegate the call to appServices.getData()', function() {
        controller = $controller('myCtrl', { $scope: $rootScope.new() });
        controller.getData();
        expect(appServices.getData).toHaveBeenCalled();
    }); 
});

Upvotes: 1

Related Questions