
Public Random Random while (i > autoFixtureStringLength & i % autoFixtureStringLength > 0) Public RandomStringOfLengthRequest(int length, string charactersToUse, Random random)

Public RandomStringOfLengthRequest(int length, string charactersToUse): this(length, charactersToUse, new Random()) Public RandomStringOfLengthRequest(int length) : this(length, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890 !?.-") Also, you can choose the subset of chars you want to use and even pass in your own random (so that you can control the seed if you need to) public class RandomStringOfLengthRequest Here's a specimen builder that can generate random strings of arbitrary length - even longer than the Guid+PropertyName strings that are by default.

New StringPropertyTruncateSpecimenBuilder(p => p.Initials, 5)) Private bool AreEquivalent(PropertyInfo a, PropertyInfo b) Return pi != null & AreEquivalent(pi, _prop) _prop = (PropertyInfo)((MemberExpression)getter.Body).Member Public StringPropertyTruncateSpecimenBuilder(Expression> getter, int length) You can generalize this SpecimenBuilder to any class and length, like so: public class StringPropertyTruncateSpecimenBuilder : ISpecimenBuilder The beauty of this approach is that it leaves the SUT alone and still doesn't affect any other string values. Public object Create(object request, ISpecimenContext context) This adds a specifically targeted convention that states that whenever the fixture is asked to create a value for a property with the name "SomeText" and of type string, the resulting string should be exactly 10 characters long: public class SomeTextBuilder : ISpecimenBuilder The third option I can think of is my favorite. This means that you can just create an instance without explicitly stating anything about the property's length: var mc = fixture.Create() However, since the default property assignment algorithm prepends the name of the property to the string, the end result will be that mc.SomeText will have a value like "SomeText3c12f144-5", so that is probably not what you want most of the time.Īnother option is to use the attribute, as Nikos points out: public class M圜lass The above customization truncates all generated strings to 10 characters.

The first option is just to constrain the base of all strings: ( Doing that, there are at least three ways to constrain string length. Personally, I very rarely use the Build method, since I prefer a convention-based approach instead. However, personally, I don't see how this is any better or easier to understand that this: var mc = fixture With the Build method itself, there aren't that many options, but you can do something like this: var constrainedText =
