undefined不是一个对象(评估' $ scope.students.pay')

时间:2016-07-06 13:58:29

标签: javascript angularjs mocha karma-mocha

我有这样的测试:

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)

});

以下是我的控制器功能:

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

为什么我收到以下错误:

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

1 个答案:

答案 0 :(得分:1)

解决方案如下:

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)

});

现在运行它,它应该工作。