Javascript 错误:预期未定义的业力等于
测试用例文件Javascript 错误:预期未定义的业力等于,javascript,angularjs,unit-testing,jasmine,karma-jasmine,Javascript,Angularjs,Unit Testing,Jasmine,Karma Jasmine,测试用例文件 describe('homeController', function() { beforeEach(module('moduleInjectionApp')); var $controller; var $rootScope; beforeEach(inject(function(_$controller_, _$rootScope_) { $controller = _$controller_('homeController', {'$scope': scope});
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);
});
});
});
控制器文件
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'));
}
错误
错误:预期未定义为等于5。
在
在UserContext。(WebContent/test/home/homeSpec.js:14:31)
在
Chrome 75.0.3770(Windows 10.0.0):执行1/1(1失败)(0.036秒/0.012秒)
总计:1次失败,0次成功
试一试
当您声明var$scope={}
,您将始终得到未定义的$scope.ID
。你需要做什么
var $scope = { ID: 5}
无论如何,在单元测试中,您不会创建一些值,然后对其进行断言。验证已定义或已修改的值。在这里,您试图声明并放置
expect
(它将始终通过)我认为范围变量无法从控制器文件访问到测试用例文件。请告诉我我是否做得不对。您声明了var$scope={}
,当您将ID
初始化为空objectOk时,您怎么能期望它被创建呢。现在我把它改成了var-scope;但还是有同样的错误。看看我的答案,如果有帮助,请告诉我\尝试此代码后,出现错误`TypeError:$controller不是UserContext上的函数。(WebContent/test/home/homeSpec.js:52:20)错误:预期未定义为等于5。在UserContext。(WebContent/test/home/homeSpec.js:56:27)在Chrome 75.0.3770(Windows 10.0.0)上:执行1/1(1失败)(0.02秒/0.011秒)`然后我在beforeach函数中将$controller作为参数传递,然后得到错误``错误:预期未定义为等于5。在UserContext。(WebContent/test/home/homeSpec.js:56:27)在Chrome 75.0.3770(Windows 10.0.0)上:执行了1次(1次失败)(0.027秒/0.014秒)总计:1次失败,0次成功``错误:[$injector:moduler])在WebContent/test/angular.min.js:7:168在Object.fb[作为injector](WebContent/test/angular.min.js:46:460)在UserContext.WorkFn(WebContent/test/angular mocks.js:3449:52)处,错误:预期未定义为等于5。在UserContext。(WebContent/test/home/homeSpec.js:56:27)这是我在karma.config.jsError中添加所有依赖项后的最新错误。错误:预期未定义为等于5。在UserContext。(WebContent/test/home/homeSpec.js:55:27)在Chrome 75.0.3770(Windows 10.0.0)上:执行1次(1次失败)(0.05秒/0.031秒)总计:1次失败,0次成功
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);
});
});
});
var $scope = { ID: 5}