Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Expected undefined to equal in karma

Test Case File

describe('homeController', function() {
beforeEach(module('moduleInjectionApp'));

var $controller;
var $rootScope;
beforeEach(inject(function(_$controller_, _$rootScope_) {
    $controller = _$controller_('homeController', {'$scope': scope});
    $rootScope = _$rootScope_;
}));

describe('$scope.ID', function() {
    it('Check the scope object', function() {
        var $scope = {};
        expect($scope.ID).toEqual(5);
    });
  });
});

Controller File

 angular.module('moduleInjectionApp').controller('homeController', homeController)
 homeController.$inject = ["$scope", "$rootScope", "$location"];
 function homeController($scope, $rootScope, $location) {
     console.log("entered homeController")
     $scope.ID = 5;
     $rootScope.loginObj = JSON.parse(localStorage.getItem('login_data'));
 }

Error

Error: Expected undefined to equal 5.
        at <Jasmine>
        at UserContext.<anonymous> (WebContent/test/home/homeSpec.js:14:31)
        at <Jasmine>

Chrome 75.0.3770 (Windows 10.0.0): Executed 1 of 1 (1 FAILED) (0.036 secs / 0.012 secs) TOTAL: 1 FAILED, 0 SUCCESS

like image 737
ANKIT DHORELIYA Avatar asked Sep 07 '25 03:09

ANKIT DHORELIYA


1 Answers

Try

describe('homeController', function() {
    beforeEach(module('moduleInjectionApp'));

    var $controller;

    beforeEach(inject(function(_$controller_){
              $controller = _$controller_;
    }));

    describe('$scope.ID', function() {
        it('Check the scope object', function() {
            var $scope = {};
            var controller = $controller('homeController', { $scope: $scope });
            expect($scope.ID).toEqual(5);
        });
    });
});

When you declare var $scope = {};, you will always get $scope.ID as undefined. You need to do

var $scope = { ID: 5}

Anyways, in unit test, you dont create some values and then expect assertions on it. You validate the values which are already defined or have been modified. Here you were trying to declare and then putting expect (which will always pass)

like image 157
Shashank Vivek Avatar answered Sep 09 '25 07:09

Shashank Vivek