Angularjs + karma + Jasmine: how to write jasmine test for child to check if parent methods are called?

I am using karma with jasmine for unit testing in my angularjs app. Given below code snippet, I am trying to check from child if parent method is called and I am running into problems as below.

ChildClass.js file

 var ChildClass= function () {
     this.id=null;
     ParentClass.apply(this,arguments);
 }; 

 function createParentObject(){             
            return new ParentClass();
        };

 ChildClass.prototype = createParentObject();

 ChildClass.prototype.testCall= function(value) {
    var self = this;  
    ParentClass.prototype.testCall.apply(this, arguments);
    self.id=1;
 }

      

ParentClass.js file

    var ParentClass= function () {
       this.title=null;
    }; 

    ParentClass.prototype.testCall= function(value) {
       var self = this;  
       self.title='test';
    }

      

My unit Test for ChildClass

  describe("MyFunction", function () {
     var parentClassMock,childClass;

       beforeEach(function () {
           var _parentClassMock=function(){
             this.title=null;
           }

          _parentClassMock.prototype.testCall= function(value) {
             var self = this;  
             self.title='test';
           }

          angular.module('childClassMocks', [])                 
                  .value('ParentClass', _parentClassMock);

         });

        beforeEach(function () {
          module('childClassMocks');
        });

      beforeEach(inject(function (_ChildClassA_,_ParentClass_) {
            parentClassMock= new _ParentClass_();
            childClass=new _ChildClass_(); 
             childClass.prototype = jasmine.createSpy("createParentObject").and.returnValue(parentClassMock);
      });
    it("test method", function () {
     spyOn(parentClassMock, 'testCall').and.callThrough();
     childClass.testCall();
     expect(parentClassMock.testCall).toHaveBeenCalled();

   });

 });

      

I am getting an error like

   Expected spy testCall to have been called.
    Error: Expected spy testCall to have been called.

      

can anyone help how to write a jasmine test case to check if the child has called the parent? I'm not sure what the code flaw is.

Any help would be appreciated.

+3


source to share





All Articles