Frage

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?

War es hilfreich?

Lösung

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)
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top