Why isn't AutoFixture working with the StringLength data annotation?

为君一笑 提交于 2020-01-03 21:01:56

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/8595498/why-isnt-autofixture-working-with-the-stringlength-data-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!