0

Below is my code , I dont want the service function to be invoked so I am using spy, but its giving error.I am not able to figure it out.

'use strict';

describe('Testing DetailCtrl\n\n\n', function() {

  beforeEach(module("safe-repository"));

  var $controller, $scope, controller;
  var services = {
   documentService:null
  };

  // Initialization before tests
  beforeEach(inject(function(_$controller_, _documentService_){
    $controller = _$controller_;
    $scope = {};
    controller = $controller('DetailCtrl', { $scope: $scope });
    services.documentService=_documentService_;

    spyOn(services.documentService, 'deleteDocument').and.callFake(function(){
           console.log("inside delete function");
    });

  }));


  describe('Testing self.deleteFile() function for different test cases\n\n', function() {


    it(' When user has access permission to delete file/doc', function(done) {
        expect(services.documentService.deleteDocument).toHaveBeenCalled();
        // Inform jasmine that the test finish here
        done();
    });

  });


});

Any help is appreciated.

shreyansh
  • 1,637
  • 4
  • 26
  • 46
  • Are you actually calling `services.documentService.deleteDocument()` anywhere? – Ankh Feb 12 '16 at 15:22
  • I am not sure , but yes I want to call services.documentService.deleteDocument() and mock it to test – shreyansh Feb 12 '16 at 15:24
  • Yeah just now I observed I think the call is not happening inside the service, what is the mistake I am doing during injecting or assigning – shreyansh Feb 12 '16 at 15:26

2 Answers2

0

you don't need ...

var services = {
   documentService:null
};

that code is just confusing matters.

you should simplify this to ...

// services.documentService=_documentService_; // WHY DO THIS??
documentService=_documentService_;

then ...

spyOn(documentService, 'deleteDocument').and.callFake ... etc

then ...

expect(documentService.deleteDocument).toHaveBeenCalled();

you might also want to try ...

spyOn(loginService, 'isSuperAdmin').and.returnValue("something");

INSTEAD OF callFake (your expect statement would remain unchanged)

ALSO ...

I assume your controller is making the expected call to this method during construction? e.g the following line makes the expected call during construction?

 controller = $controller('DetailCtrl', { $scope: $scope });

In other words your controller should look something like ...

app.controller("DetailCtrl", function($scope, documentService) {
    // some other code
    documentService.deleteDocument(); // MAKE SURE THIS CODE IS ACTUALLY BEING HIT IF ITS WRAPPED IN A CONDITIONAL STATEMENT
    // some other code
});
danday74
  • 52,471
  • 49
  • 232
  • 283
0

Try this, it might help you :)

'use strict';

describe('Testing DetailCtrl\n\n\n', function() {

var $controller, scope, ctrl, mockService;

beforeEach(module("safe-repository"));

beforeEach(inject(function($rootScope, _$controller_){
 scope = $rootScope.$new();
 function del() {
    //your return value
 }
 mockService = {
   deleteDocument: del
 }
 $controller = _$controller_;
 }));

 function initController(){
   ctrl = $controller('DetailCtrl', {
    $scope: scope,
    documentService: mockService
   });
 }


 it(' When user has access permission to delete file/doc', function() {
     spyOn(documentService,'deleteDocument').and.callThrough();
     initController();
     expect(mockService.deleteDocument).toHaveBeenCalled();
 });

});


});
Darshuu
  • 501
  • 5
  • 9