Jimi
Jimi

Reputation: 1907

method does not exist when calling spyOn

I'm trying to use Jasmine spyOn to call an angularjs controller function. I keep getting the following error:

submit() method does not exist.

What am I missing in order for this to work?

    describe('myCtrler', function() {
    beforeEach(module('myModule'));
    var scope,ctrl;
    beforeEach(inject(function($rootScope, $controller) {
      scope = $rootScope.$new();
        spyOn(scope, "submit");
      ctrl = $controller('myCtrler', {
          $scope: scope
      });
    }));
    it('controller defined', inject(function() {
        expect(ctrl).toBeDefined();
    }));
    it('controller function', function() {
        expect(submit).toBeDefined();
    });
    });
   angular.module('myModule').controller('myCtrler',function($scope){
       var vm = this;
       vm.submit = function() {
       };
     });

Upvotes: 1

Views: 9637

Answers (1)

user7353226
user7353226

Reputation: 55

as per the question you missed to bind the method to the scope. so you need to make as below. Hope it helps.

it('controller function', function() {
            expect(ctrl.submit).toBeDefined();
        });

Upvotes: 1

Related Questions