Enums

Nov 4, 2009 at 3:29 PM

Do you have a sample on how to map Enums?  We're getting an error because there's no default constructor.

Nov 4, 2009 at 3:41 PM

Nevermind, we're idiots.

 

Coordinator
Nov 5, 2009 at 7:16 AM

Would it be correct of me to interpret your last post as you figured it out already?

Otherwise, I would be more than happy to help.

Nov 5, 2009 at 3:33 PM

Yeah, in this case, it was just us looking at the wrong bit of code.  I was able to use fixture.Register<MyEnumType>.   It wasn't able to auto generate it though, as it tried to new it up.

Coordinator
Nov 5, 2009 at 7:16 PM

Good to hear you got it working :) Register is the method I would use myself.

In case you are interested, I have actually been tracking the enum issue for a while, as witnessed by this issue.

Developer
Jan 22, 2010 at 9:10 AM
Edited Jan 22, 2010 at 9:10 AM

You can use the Resolver extensibility point to auto-create Enums:

 

_fixture = new Fixture();
_fixture.Resolver = CustomResolver;

private object CustomResolver(Type arg)
{
  if (arg.IsEnum)
  {
    var values = Enum.GetValues(arg);
    return values.GetValue(new Random().Next(values.Length));
  }

  return null;
}

This construct returns a random Enum member of the specified Enum type each time AutoFixture is asked to create an Enum.

 

Coordinator
Jan 24, 2010 at 8:31 PM

Pretty creative use of the Resolver callback that I hadn't even thought of myself :)

One thing to be aware of, though: in the current release (RC2) we've hidden the Resolver callback (using EditorBrowsableState.Never) as we have some future plans on how to make AutoFixture way more extensible than it currently is. The Resolve callback may or may not survive in version 2.0, but if it doesn't survive, you can be sure it's going to be replaced with something much more powerful :)

Until then, this is an excellent and easy solution to the no enum support issue that I will go adopt myself when I get to work tomorrow.

Thanks for sharing.

May 28, 2010 at 8:27 PM

Now as the Resolver call has gone missing. Could you please point to the alternate method? Thanks in advance.

Coordinator
May 28, 2010 at 9:22 PM
Edited May 28, 2010 at 9:22 PM

Thanks for asking.

Resolver is not missing yet - it's just hidden (using EditorBrowsableState.Hidden) because it will eventually be removed to be replaced with something more open and extensible. However, in AutoFixture 1.1 it's still available although invisible to IntelliSense. Just type it out manually, and the code will still compile.

I'll post a note on how to replace the Resolver when AutoFixture 2.0 comes out.

May 29, 2010 at 6:49 PM

Thanks Mark. AutoFixture is a great tool, thanks for sharing it with the community.

Aug 18, 2011 at 8:33 AM
Edited Aug 18, 2011 at 8:33 AM

I'm also interested in this but over a year has passed by without further response. So what happened to the Resolver?

Coordinator
Aug 18, 2011 at 8:39 AM

AutoFixture 2.x has a completely revamped extensibility model revolving around the Customizations and ResidueCollectors properties. The easiest entry point into this is to use the Customize, Register and Inject methods, but these days I personally prefer more convention-based customizations.

What would you like AutoFixture to do for you?

Aug 18, 2011 at 9:18 AM
Edited Aug 18, 2011 at 9:22 AM

Just a simple task: I'd like to have instances of business objects with a property of the type Color (among others which are not relevant for this problem).  When I try to let Autofixture create objects from this it throws because Color has no default constructor.

Oh and by the way: I can't use AutoMoqCustomization because Autofixture wants a Moq version of 3.1.something but I only have Version 4 of Moq.

Coordinator
Aug 18, 2011 at 9:27 AM

How do you want the Color defined?

The simplest is just to inject a Color instance into the fixture before you start creating objects:

fixture.Inject(Color.Black);

This will reuse the same Color instances for all created objects.

Alternatively, you can use the Register method to specify a function that creates new instances every time it's invoked. That might look something like this:

fixture.Register(CreateColor);

Where CreateColor is a method with this signature:

Color CreateColor()

You can also use a code block (lambda expression) if you prefer.

Personally, I'd implement the desired behavior in a custom ISpecimenBuilder - it's actually quite easy.

Aug 18, 2011 at 10:27 AM

Thank you, I will try that. :-)