I have an object containing a char property. For my test, I need to have that property set to anything but I or R. Is there anything, ideally native to AutoFixture, that I could do to have cleaner, briefer code than what I came up with?
This is a representation of the object
public class MyObject
{
public char MyProperty {get; set;}
}
And the solution I'm using
MyObject = _fixture.Create<MyType>();
if ((new char[] { 'I', 'R' }).Contains(MyObject.MyProperty))
MyObject.MyProperty = new Generator<char>(_fixture).Where(c => !new char[] { 'I', 'R' }.Contains(c)).First();
ElementsBuilder seems like a good candidate here, so long as you're willing to create a whilelist of acceptable characters, rather than a blacklist.
Here's a quick sample, using your MyObject
definition.
var fixture = new Fixture();
// A-Z, minus I, R.
fixture.Customizations.Add(new ElementsBuilder<char>("ABCDEFGHJKLMNOPQSTUVWXYZ"));
var createdCharacters = fixture
.Create<Generator<MyObject>>()
.Take(1000)
.Select(f => f.MyProperty)
.ToList();
CollectionAssert.DoesNotContain(createdCharacters, 'I');
CollectionAssert.DoesNotContain(createdCharacters, 'R');
If you need any more nuance than that (e.g., other objects can use I or R characters), a custom ISpecimenBuilder
is the way to go.
I recommend using custom initialization logic with the Fixture.Customize<T>()
method, this give you an instance of your type composer and let's you set values for T's properties. This has the added benefit of restricting the logic to a specific type, unlike ElementBuilder which will generate chars for every type.
fixture.Customize<MyType>(composer => composer.With(myType => myType.MyProperty, "[custom value]"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With