Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6983537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:25:56+00:00 2026-05-27T18:25:56+00:00

I’m trying again to upgrade to AutoFixture 2, and I’m running into a problem

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:25:57+00:00Added an answer on May 27, 2026 at 6:25 pm

    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)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
i got an object with contents of html markup in it, for example: string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.