【发布时间】:2020-02-13 13:27:33
【问题描述】:
测试用例文件
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'));
}
错误
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):执行 1 of 1 (1 FAILED) (0.036 secs / 0.012 secs) 总计:1 次失败,0 次成功
【问题讨论】:
-
我认为范围变量无法从控制器文件访问到测试用例文件。如果我做错了,请告诉我。
-
你声明了
var $scope = {},你怎么能指望ID在你初始化为空对象时被创建 -
好的。所以现在我把它改成了 var scope;
-
但仍然出现同样的错误。
-
看看我的回答,如果有帮助,请告诉我。 \
标签: javascript angularjs unit-testing jasmine karma-jasmine