Search code examples
javascriptangularjsmocha.jskarma-mocha

undefined is not an object (evaluating '$scope.students.pay')


I have a test like this:

it('calls the stuFun method', function () {
     $scope.stuFun();
     expect($scope.stuFun).to
            .have.been.calledOnce;
     expect($scope.students.pay).to.not.equal(null);
     assert.isNotTrue($scope.students.pay)

});

Below is my controller function:

$scope.stuFun = function() {
    $scope.students.pay = false;
};

Why am i getting the below error:

undefined is not an object (evaluating '$scope.students.pay')

Solution

  • Solution is attached below:

    it('calls the stuFun method', function () {
         $scope.students = {};
         $scope.stuFun();
    
         expect($scope.stuFun).to
                .have.been.calledOnce;
         expect($scope.students.pay).to.not.equal(null);
         assert.isNotTrue($scope.students.pay)
    
    });
    

    Now run it, it should work.