1

I am getting error : Expecting undefined to be defined. When i try on Visual Studio its working fine, but not on the test site that i am working on. HTML Code :

<html>
<head>
<script src="lib/angularjs/angular.min.js"></script>
<script src="lib/angularjs/angular-mocks.js"></script>
<script src="index.js" type="text/javascript"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<input type="number" ng-model="number1"><br><br>
<input type="number" ng-model="number2"><br><br>
{{number1}} + {{number2}} = <span style="color:green" type="text">{{number1 + number2}}</span> which is an
<span ng-bind="odd_even(number1 + number2)"></span> number
<br><br>
{{number1}} - {{number2}} = <span style="color:green" type="text">{{number1 - number2}}</span> which is 
<span ng-bind="odd_even(number1 + number2)"></span> number
<br><br>
</div>
</body>
</html>

Controller JS Code :

    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
        //add your code here
        $scope.odd_even = function(result) {
            if(result % 2 == 0) {
                return 'even';
}
            else if(result % 2 == 1){
                return 'odd';
            }
        }
    });

Error :

AngularJS Test Controller: Testing: Check the scope object number1 is defined or not FAILED                   
        Expected undefined to be defined.                                                                                                   
            at UserContext.<anonymous> (test/index_test.js:19:36)                                                                           
Node.js (linux; U; rv:v8.9.4) AngularJS Test Controller: Testing: Check the scope object number2 is defined or not FAILED                   
        Expected undefined to be defined.     

Please help !!!

2
  • Right up front I'll admit I'm not familiar with how angular-mocks works for testing, but neither number1 nor number2 are defined on $scope in the controller. Is that perhaps a requirement for the testing harness to work properly? Commented Jun 14, 2018 at 21:38
  • Yes, thats right. Can you please help me with the same. Commented Jun 28, 2018 at 23:53

1 Answer 1

2

Could you please try like this and let me know if any issues?

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    //add your code here
    $scope.number1 = 100;
    $scope.number2 = 11;

    $scope.result = $scope.number1+$scope.number2;

    $scope.odd_even = function(result)
    {

        if(result%2==0)
        {
            return "even";
        }
        else if(result % 2 == 1)
        {
            return "odd";
        }
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.