AutoFixture: how to CreateAnonymous from a System.Type
Asked Answered
C

2

23

I need to create an object from AutoFixture using nothing more than a System.Type. However, there doesn't appear to be an overload of CreateAnonymous() that simply takes a type. They all expect a compile time generic T. Is there a way to convert a System.Type to T?

Edit with usage details:

I'm using AutoMapper, which has a hook for injecting components to support complex mapping scenarios:

void ConstructServicesUsing(System.Func<Type,object> constructor)

As you can see from the signature, clients can register a Func which AutoMapper invokes anytime it needs an injected service (mostly ValueResolver implementations).

In production builds, this method calls into my StructureMap container to retrieve a component. However, when unit testing my mapping code, I must provide stub implementations otherwise AutoMapper throws an exception. Since I'm using AutoFixture + Moq as my automocking container, it seems natural to let AF new up a fully hydrated stub, so I can concentrate on writing unit test code.

Contravallation answered 14/5, 2013 at 15:12 Comment(1)
Can you tell more about your usage scenario? In which kind of test you don't know your type compile time so there you cannot use the CreateAnonymous method?Chinoiserie
S
41

It's possible, but intentionally hidden, since you should very rarely need to do this:

var specimen = new SpecimenContext(fixture).Resolve(type);

There are tons of extensibility points in AutoFixture that, more often than not, provide a better alternative than a weakly typed Create method. What are you trying to accomplish?

Senior answered 14/5, 2013 at 15:38 Comment(5)
Thanks Mark. Worked like a charmContravallation
Thanks as well, Mark. In my case I am writing a single helper to check all of an instance's properties to ensure that they properly raise the PropertyChanged event. May be a bit hacky, but I'd prefer to write the test once in a generic way and then not have to worry about it.Artifice
@MattKlein You should take a look at AutoFixture.Idioms, then. You could make that an IdiomaticAssertion.Senior
I also have a scenario where I'm using it: I'm testing a web api action method which catches exceptions from a (mocked) service and returns appropriate http status codes. My ninject TestCases specify the type of exceptions thrown, which autofixture then needs to generate. Again, it's better to do this with 10 test cases than 10 separate test methods.Stigma
@Stigma That sounds like it could be addressed better by either AutoFixture.Idioms or by generic tests.Senior
D
1

You have to use reflection to create the correct MethodInfo and call it. See this answer on how to do that: How to call generic method with a given Type object?

Dewdrop answered 14/5, 2013 at 15:15 Comment(1)
I know it's a 5 year old answer so the api might have changed but it's actually a little more complex than the linked answer since the Create<T> is an extensions method: github.com/AutoFixture/AutoFixture/blob/master/Src/AutoFixture/…Noncompliance

© 2022 - 2024 — McMap. All rights reserved.