如何在 AutoFixture 中创建样本构建器以始终返回与正则表达式匹配的字符串? [英] How do I create a specimen builder in AutoFixture to always return a string that matches a Regex?
问题描述
我正在尝试在我的单元测试中使用使用 AutoFixture 的约定.我有一个密码值对象,如图所示:
I am trying to use conventions in my unit tests which make use of AutoFixture. I have a Password value object as shown:
public class Password : SemanticType<string>
{
private int _daysPasswordValid;
public Password(string password) : base(IsValid, password)
{
Guard.NotNullOrEmpty(() => password, password);
Guard.IsValid(() => password, password, IsValid, "Invalid Password");
DateCreated = DateTime.Now;
}
static bool IsValid(string candidate)
{
//password cannot contain be whitespace
if (string.IsNullOrWhiteSpace(candidate))
return false;
const string passwordPattern = @"^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&=()-*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{20}$";
var match = Regex.Match(candidate, passwordPattern);
return match.Success;
}
public DateTime DateCreated { get; private set; }
public int DaysPasswordValid
{
get { return _daysPasswordValid; }
set
{
const int minimunDays = 0;
const int maximumDays = 90;
if (value <= maximumDays && value > minimunDays)
_daysPasswordValid = value;
else
throw new ArgumentOutOfRangeException(
"Password must be valid more than {0} and no more than {1} days.".Fmt(minimunDays, maximumDays));
}
}
public static implicit operator string(Password password)
{
return password.Value;
}
public static explicit operator Password(string value)
{
return new Password(value);
}
}
我希望能够基于 Password 类创建一个装置并使用有效密码创建该装置,换句话说,一个与 Password 类中的 RegEx 模式匹配的密码.我一直在查看 AutoFixture 中可用的 RegularExpressionGenerator,但还没有完全开始工作.这是建造者迄今为止的成果:
I would like to be able to create a fixture based on the Password class and have the fixture created with a valid password, in other words, a password that matches the RegEx pattern in the Password class. I have been looking at the RegularExpressionGenerator available in AutoFixture but haven't quite gotten things to work. Here is the builder have so far:
public class ValidPasswordBuilder : ISpecimenBuilder
{
public ValidPasswordBuilder(string regularExpressionRequest)
{
PasswordRegularExpression = regularExpressionRequest;
}
public string PasswordRegularExpression { get; private set; }
public object Create(object request, ISpecimenContext context)
{
var pi = request as ParameterInfo;
if (pi != null && pi.Name == "password" && pi.ParameterType == typeof(string))
{
var generator = new RegularExpressionGenerator();
var regExRequest = new RegularExpressionRequest(PasswordRegularExpression);
var result = generator.Create(regExRequest, context);
return result.ToString();
}
return new NoSpecimen(request);
}
}
测试如下:
[Fact]
public void ValidPasswordMatches()
{
var fixture = new Fixture();
fixture.Customizations.Add(new ValidPasswordBuilder(PasswordPattern));
Action a = () => fixture.Create<Password>();
a.ShouldNotThrow<ArgumentException>();
}
我可以按照上面的设置运行测试,并且它通过了,但是如果我调试它,我会在 Password 类(通过 Guard 子句)的 IsValid 函数中收到一个错误,指出从构建器返回的密码是:
I can run the test as setup above, and it passes, but if I debug it, I get an error in the IsValid function in the Password class (through the Guard clause) that says that the password returned from the builder is:
Ploeh.AutoFixture.Kernel.NoSpecimen
Ploeh.AutoFixture.Kernel.NoSpecimen
我似乎从未得到与 RegEx 模式匹配的结果.我觉得我很接近了,但需要一些帮助来克服困难.
I never seem to get a result that matches the RegEx pattern. I feel like I'm close, but need some help getting over the hump.
谢谢!
推荐答案
看起来,您使用的模式不受支持:
As it looks like, the pattern you use is not supported:
^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&=()-*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{20}$
尝试使用不同的正则表达式.
Try with a different Regular Expression.
编辑:或者尝试执行这个 或 this,以便将 AutoFixture 配置为使用不同的正则表达式,无需更改 Password
类.
Edit: Or try doing this or this, in order to configure AutoFixture to use a different Regular Expression, without changing the Password
class.
AutoFixture 通过应用 dk.brics.automaton.
AutoFixture turns Regular Expressions into Automatons by applying the algorithms of dk.brics.automaton.
您可以使用不同的模式或使用不同的引擎将正则表达式反转为自动机.例如,您可以使用 Rex 引擎.
You may use a different pattern or use a different engine to reverse the Regular Expression into an Automaton. As an example, you can use the Rex engine.
尽管如此,即使使用 Rex,您的正则表达式也不支持,因为 Rex 目前不支持以下结构:
Though, even with Rex your Regular Expression is not supported, as the following constructs are currently not supported by Rex:
锚点\G、\b、\B、命名组、前瞻、后视、尽可能少的量词、反向引用、条件交替、替换.
anchors \G, \b, \B, named groups, lookahead, lookbehind, as-few-times-as-possible quantifiers, backreferences, conditional alternation, substitution.
如果您想使用 AutoFixture 尝试 Rex,您可以使用以下生成器:
If you want to try Rex with AutoFixture you can use the following generator:
internal class RexRegularExpressionGenerator : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
if (request == null)
return new NoSpecimen();
var regularExpressionRequest = request as RegularExpressionRequest;
if (regularExpressionRequest == null)
return new NoSpecimen();
var pattern = regularExpressionRequest.Pattern;
try
{
var regex = RexEngine
.GenerateMembers(
new RexSettings(pattern) { k = 1 })
.FirstOrDefault();
if (Regex.IsMatch(regex, pattern))
return regex;
}
catch (ArgumentException)
{
return new NoSpecimen(request);
}
return new NoSpecimen(request);
}
}
据我所知,您只能将 Rex 作为 .NET 应用程序 (.exe) 文件下载,然后您可以像项目中的任何其他托管程序集一样引用该文件.
As far as I can tell, you can download Rex only as a .NET application (.exe) file which you can then reference like any other managed assembly in your project.
这篇关于如何在 AutoFixture 中创建样本构建器以始终返回与正则表达式匹配的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!