为什么 AutoFixture 不支持 StringLength 数据注释?

发布于 2024-12-22 06:38:12 字数 778 浏览 1 评论 0原文

我再次尝试升级到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静被遗忘 2024-12-29 06:38:12

感谢您报告此事!

此行为是设计使然(其原因基本上是属性本身的描述)。

通过在数据字段上应用 [StringLength(255)],基本上意味着允许有 0 到 255 个字符。

根据msdn上的描述,StringLengthAttribute类:

当前版本 (2.7.1) 基于 .NET Framework 3.5 构建。从 2.4.0 开始支持 StringLengthAttribute 类。

话虽这么说,创建的实例是有效的(只是第二个断言语句不是)。

这是一个通过测试,使用 验证器验证创建的实例System.ComponentModel.DataAnnotations 命名空间中的 类:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using NUnit.Framework;
using Ploeh.AutoFixture;

public class Tests
{
    [Test]
    public void GetAll_HasContacts()
    {
        var fixture = new Fixture();
        var bleh = fixture.CreateAnonymous<Bleh>();

        var context = new ValidationContext(bleh, serviceProvider: null, items: null);

        // A collection to hold each failed validation.
        var results = new List<ValidationResult>();

        // Returns true if the object validates; otherwise, false.
        var succeed = Validator.TryValidateObject(bleh, context, 
            results, validateAllProperties: true);

        Assert.That(succeed, Is.True);  // pass
        Assert.That(results, Is.Empty); // pass
    }

    public class Bleh
    {
        [StringLength(255)]
        public string Foo { get; set; }
        public string Bar { get; set; }
    }
}

更新 1

虽然创建的实例有效,但我相信可以对其进行调整以选择随机数里面的范围(0 - 最大长度),因此用户永远不会得到空字符串。

我还在论坛此处创建了讨论。

更新 2

如果您升级到 AutoFixture 版本 2.7.2(或更高版本),原始测试用例现在将通过。

[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);  // pass (version 2.7.2 or newer)
}

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:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using NUnit.Framework;
using Ploeh.AutoFixture;

public class Tests
{
    [Test]
    public void GetAll_HasContacts()
    {
        var fixture = new Fixture();
        var bleh = fixture.CreateAnonymous<Bleh>();

        var context = new ValidationContext(bleh, serviceProvider: null, items: null);

        // A collection to hold each failed validation.
        var results = new List<ValidationResult>();

        // Returns true if the object validates; otherwise, false.
        var succeed = Validator.TryValidateObject(bleh, context, 
            results, validateAllProperties: true);

        Assert.That(succeed, Is.True);  // pass
        Assert.That(results, Is.Empty); // pass
    }

    public class Bleh
    {
        [StringLength(255)]
        public string Foo { get; set; }
        public string Bar { get; set; }
    }
}

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).

[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);  // pass (version 2.7.2 or newer)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文