【发布时间】:2014-07-11 16:21:02
【问题描述】:
我正在尝试对我的角度控制进行测试。如何测试函数中的变量?
示例:
$scope.onFileSelect = function($files) {
$scope.selectedFiles = [];
$scope.progress = [];
if ($scope.upload && $scope.upload.length > 0) {
for (var i = 0; i < $scope.upload.length; i++) {
if ($scope.upload[i] !== null) {
$scope.upload[i].abort();
}
}
}
$scope.upload = [];
$scope.uploadResult = [];
$scope.selectedFiles = $files;
$scope.dataUrls = [];
if ($scope.selectedFiles.length==1) {
for ( var i = 0; i < $files.length; i++) {
var $file = $files[i];
if (window.FileReader && $file.type.indexOf('image') > -1) {
var fileReader = new FileReader();
fileReader.readAsDataURL($files[i]);
var loadFile = function(fileReader, index) {
fileReader.onload = function(e) {
$timeout(function() {
$scope.dataUrls[index] = e.target.result;
});
};
}(fileReader, i);
}
$scope.progress[i] = -1;
if ($scope.uploadRightAway) {
$scope.start(i);
}
}
}
else{
$scope.selectedFiles = null;
$scope.clientmsg={};
$scope.errormessageheader="";
$scope.uploadErrors="";
$scope.UploadSuccess="";
auxArray = [""];
$scope.uploadErrors =auxArray;
$scope.errormessageheader="Only_submit_one_file";
}
};
还有我的测试...
describe('Unit: routeAssignUpload_Ctrl', function () {
beforeEach(angular.mock.module('primaryDistributionApp'));
var ctrl, scope;
// inject the $controller and $rootScope services
// in the beforeEach block
beforeEach(inject(function($controller, $rootScope) {
// Create a new scope that's a child of the $rootScope
scope = $rootScope.$new();
// Create the controller
ctrl = $controller('routeAssignUpload_Ctrl', {
$scope: scope
});
}));
it('probando funcion onFileSelect',function(){
expect(scope.onFileSelect).toBeDefined();
var onFileSelect1 = new scope.onFileSelect;
spyOn(onFileSelect1).andCallThrough();
expect(scope.upload).toBeDefined();
});
})
结果是“TypeError: 'undefined' is not an object”(评估 '$scope.selectedFile s.长度') 我不明白为什么,谁能帮帮我?
【问题讨论】:
标签: javascript angularjs unit-testing testing