This project is read-only.

CreateAnonymous and ISet<T> and the like

Mar 16, 2010 at 2:49 PM


Is there a recommended way to have AutoFixture create a HashedSet<> with the correct generic type argument when encountering an ISet<>?

And thank you for what seems to be a great project! :)


Mar 16, 2010 at 2:51 PM

Oh. I should have looked at the issue tracker, I see... :)

Sorry for the inconvenience.


Mar 16, 2010 at 2:56 PM

As you seem to have found out, AutoFixture doesn't currently support open generics, although it's on the radar for a future release.

We are currently writing a new, much more flexible and pluggable kernel for AutoFixture 2.0, and although I haven't yet verified it, I think that defining such a map from ISet<> to HashedSet<> should become possible (I shy away from using the word trivial because I've heard too many horror stories about open generics, but that capability should become more attainable.)

Mar 16, 2010 at 2:58 PM

Until AutoFixture 2.0, however, you may be able to use the Resolver callback to implement this mapping manually. See Peter's reply in this discussion:

The only caveat is that the Resolver callback may disappear in AutoFixture 2.0 to be replaced by something more flexible.

Mar 16, 2010 at 3:39 PM

Thank you for the quick responses!

I'll check out Peter's solution. Thanks.


Jan 10, 2011 at 11:23 AM

I also have encountered this as an issue and didn't think to look at the issue tracker!! And therefore myself (@james_m_allen and @rea_andrew) have made a GenericCollectionMappingSpecimenBuilder which sets up some default mappings and then allows you to add additional mappings:

new Fixture().AddTypeMapping(typeof (ISet<>), typeof (HashedSet<>));

For us it was a case of ARRGGGHHH it doesn't do this!! lets write it! But being very new to AutoFixture it probably could be refined somewhat ;) also although it creates the appropriate collection it doesn't populate it with any entities.

What's the best way to get this reviewed?


Jan 12, 2011 at 1:42 PM

Have you already seen this?

Also, to map interfaces to concrete types, you could use this technique:

I can take a look at your GenericCollectionMappingSpecimenBuilder if you'd like. There are many different ways we could do this:

  • Send me an email
  • Ask a question on Stack Overflow
  • Use a code snippet sharing service
  • Send me a pull request
  • Something else I haven't thought of yet... :)