Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixture Constraints on property autogeneration

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();
like image 502
bkqc Avatar asked Dec 05 '19 15:12

bkqc


2 Answers

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.

like image 144
Jeff Dammeyer Avatar answered Nov 10 '22 15:11

Jeff Dammeyer


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]"));
like image 37
sspaniel Avatar answered Nov 10 '22 15:11

sspaniel