为什么 AutoFixture 不支持 StringLength 数据注释?
我再次尝试升级到 AutoFixture 2,但遇到了数据注释问题我的对象。下面是一个示例对象:
public class Bleh
{
[StringLength(255)]
public string Foo { get; set; }
public string Bar { get; set; }
}
我尝试创建一个匿名 Bleh
,但带有注释的属性为空,而不是用匿名字符串填充。
[Test]
public void GetAll_HasContacts()
{
var fix = new Fixture();
var bleh = fix.CreateAnonymous<Bleh>();
Assert.That(bleh.Bar, Is.Not.Empty); // pass
Assert.That(bleh.Foo, Is.Not.Empty); // fail ?!
}
根据 Bonus Bits 的说法,从 2.4.0 开始应该支持 StringLength
,尽管即使不支持,我也不会期望出现空字符串。我正在使用 NuGet 中的 v2.7.1。我是否错过了创建数据注释对象所需的某种自定义或行为?
I'm trying again to upgrade to AutoFixture 2, and I'm running into a problem with the data annotations on my objects. Here's an example object:
public class Bleh
{
[StringLength(255)]
public string Foo { get; set; }
public string Bar { get; set; }
}
I'm attempting to create an anonymous Bleh
, but the property with the annotation is coming up empty rather than being populated with an anonymous string.
[Test]
public void GetAll_HasContacts()
{
var fix = new Fixture();
var bleh = fix.CreateAnonymous<Bleh>();
Assert.That(bleh.Bar, Is.Not.Empty); // pass
Assert.That(bleh.Foo, Is.Not.Empty); // fail ?!
}
According to Bonus Bits, StringLength
should be supported as of 2.4.0, though even if it wasn't supported I wouldn't expect an empty string. I am using v2.7.1 from NuGet. Have I missed some sort of customization or behavior that is necessary to create data-annotated objects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢您报告此事!
此行为是设计使然(其原因基本上是属性本身的描述)。
通过在数据字段上应用 [StringLength(255)],基本上意味着允许有 0 到 255 个字符。
根据msdn上的描述,StringLengthAttribute类:
指定数据中允许的最大字符长度
场地。 [.NET Framework 3.5 ]
指定字符的最小和最大长度
允许在数据字段中。 [.NET Framework 4 ]
当前版本 (2.7.1) 基于 .NET Framework 3.5 构建。从 2.4.0 开始支持 StringLengthAttribute 类。
话虽这么说,创建的实例是有效的(只是第二个断言语句不是)。
这是一个通过测试,使用 验证器验证创建的实例System.ComponentModel.DataAnnotations 命名空间中的 类:
更新 1:
虽然创建的实例有效,但我相信可以对其进行调整以选择随机数里面的范围(0 - 最大长度),因此用户永远不会得到空字符串。
我还在论坛此处创建了讨论。
更新 2:
如果您升级到 AutoFixture 版本 2.7.2(或更高版本),原始测试用例现在将通过。
Thanks for reporting this!
This behavior is by design (the reason for that is, basically, the description of the attribute itself).
By applying [StringLength(255)] on a data field it basically means that it is allowed to have 0 up-to 255 characters.
According to the description on msdn, the StringLengthAttribute Class:
Specifies the maximum length of characters that are allowed in a data
field. [.NET Framework 3.5]
Specifies the minimum and maximum length of characters that are
allowed in a data field. [.NET Framework 4]
The current version (2.7.1) is built on .NET Framework 3.5. The StringLengthAttribute class is supported as of 2.4.0.
That being said, the created instance is valid (it's just that the second assertion statement isn't).
Here is a passing test that validates the created instance using the Validator class from the System.ComponentModel.DataAnnotations namespace:
Update 1:
While the created instance is valid, I believe that this could be adjusted to pick a random number inside the range (0 - maximumLength) so the user never gets an empty string.
I have also created a discussion at the forum here.
Update 2:
The original test case will now pass if you upgrade to AutoFixture version 2.7.2 (or newer).