【发布时间】:2014-02-19 17:15:43
【问题描述】:
我正在尝试使用 Jasmine 框架对这行 javascript 进行单元测试:
$scope.validerStyleTag = function() {
$scope.currentTag = JSON.parse($scope.tagList);
for (var i = $scope.listTags.length - 1; i >= 0; i--) {
if ($scope.listTags[i]._id === $scope.currentTag._id) {
$scope.tagID = $scope.listTags[i]._id;
$scope.listTags[i].disabled = true;
break;
}
}
// var textestyler = angular.element(document.querySelector('#style-affected-add'))[0].outerHTML;
// var debut = textestyler.substring(textestyler.indexOf('<p'), textestyler.indexOf('>') + 1);
// var texteFinal = debut + '</p>';
var mytext = '<p data-font="' + $scope.policeList + '" data-size="' + $scope.tailleList + '" data-lineheight="' + $scope.interligneList + '" data-weight="' + $scope.weightList + '" data-coloration="' + $scope.colorList + '"> </p>';
$scope.tagStyles.push({
id_tag: $scope.currentTag._id,
style: mytext,
label: $scope.currentTag.libelle,
police: $scope.policeList,
taille: $scope.tailleList,
interligne: $scope.interligneList,
styleValue: $scope.weightList,
coloration: $scope.colorList,
});
angular.element($('.shown-text-add').text($('.shown-text-add').text()));
angular.element($('#style-affected-add').removeAttr('style'));
$scope.colorationCount = 0;
$scope.tagList = null;
$scope.policeList = null;
$scope.tailleList = null;
$scope.interligneList = null;
$scope.weightList = null;
$scope.colorList = null;
};
测试的问题在第一行:
$scope.currentTag = JSON.parse($scope.tagList);
当我开始像这样编写单元测试并调用我的函数时:
it('ProfilesCtrl:validerStyleTag should set validerStyleTag ', inject(function() {
expect($scope.validerStyleTag).toBeDefined();
$scope.validerStyleTag();
}));
业力给了我一个意外错误:TypeError: Attempted to assign to readonly property at workFn (/home/app/bower_components/angular-mocks/angular-mocks.js:2107)
非常欢迎提出想法或反馈!!
【问题讨论】:
-
您是否在使用带有“旧”版本 Angular 的 PhantomJS?有一个错误导致了最近已修复的错误。如果我没看错的话,它看起来像是在 1.2.12 中修复的。 github.com/angular/angular.js/pull/5047
-
@CraigSquire 谢谢你的回答,那么我该如何更新我的 Angularjs 版本呢?和 phantomJs 版本?
-
顺便说一句,实际上是 angular-mocks 有错误。
-
让我知道更新是否修复了它。
-
不错。我上周遇到了同样的问题。
标签: javascript json angularjs jasmine karma-runner