最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error: Expected undefined to equal in karma - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Feb 18, 2020 at 12:06 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Feb 13, 2020 at 13:27 ANKIT DHORELIYAANKIT DHORELIYA 951 gold badge2 silver badges7 bronze badges 5
  • I think scope variable is not accessible from controller file to test case file. Please tell me if i'm doing it in wrong way. – ANKIT DHORELIYA Commented Feb 14, 2020 at 7:21
  • You declared var $scope = {} , how can you expect the ID to get created when you initialized it as empty object – Shashank Vivek Commented Feb 14, 2020 at 8:25
  • Ok. So now i have changed it like var scope; – ANKIT DHORELIYA Commented Feb 14, 2020 at 9:57
  • but still getting same error. – ANKIT DHORELIYA Commented Feb 14, 2020 at 9:58
  • Take a look at my answer and let me know your if it helped. \ – Shashank Vivek Commented Feb 14, 2020 at 10:32
Add a ment  | 

1 Answer 1

Reset to default 2

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)

发布评论

评论列表(0)

  1. 暂无评论