我正在尝试在javascript中学习测试驱动开发方面实现打印钻石的功能.
Diamond.prototype.outerSpace = function (current, widest) {
var currentValue = this.getIndexOf(current);
var widestValue = this.getIndexOf(widest);
if (currentValue > widestValue) {
throw new Error('Invalid combination of arguments');
}
var spaces = widestValue - currentValue;
return new Array(spaces + 1).join(' ');
};
Run Code Online (Sandbox Code Playgroud)
我在错误处理方面遇到问题.如果currentValue大于widestValue,则上述函数应抛出错误.
这是我的代表测试/规范的片段:
it ("should throw an exception, if it is called with D and C", function () {
var outerSpace = diamond.outerSpace.bind(diamond, 'D', 'C');
expect(outerSpace).toThrow('Invalid combination of arguments');
});
Run Code Online (Sandbox Code Playgroud)
我也试过在expect(..)中使用匿名函数,但这也没有用.
控制台消息是:预期函数抛出'Inval ...'但它抛出错误:参数组合无效.
我不明白,我应该怎么做这些信息.
编辑:这很奇怪,因为它与Jasmine v.1.3一起使用,但它与jasmine v.2.3 ie或与业力无关,尽管代码基于茉莉.
如上所述,我将尝试在注册过程中使用某个额外规则来验证密码.如果密码至少有一个数字,一个字母和一个特殊字符,则额外规则应该是密码验证.
我解决这个问题的方法我创建了一个名为validators.py的文件.
from django.core.exceptions import ValidationError
class CustomPasswortValidator:
def validate(value):
# check for digit
if not any(char.isdigit() for char in value):
raise ValidationError(_('Password must contain at least 1 digit.'))
# check for letter
if not any(char.isalpha() for char in value):
raise ValidationError(_('Password must contain at least 1 letter.'))
# check for special character
special_characters = "[~\!@#\$%\^&\*\(\)_\+{}\":;'\[\]]"
if not any(char in special_characters for char in value):
raise ValidationError(_('Password must contain at least 1 letter.'))
Run Code Online (Sandbox Code Playgroud)
我的自定义注册表单如下所示:
from django import forms
from django.contrib.auth.forms …Run Code Online (Sandbox Code Playgroud) django ×1
jasmine ×1
javascript ×1
karma-runner ×1
passwords ×1
python ×1
registration ×1
tdd ×1
validation ×1