Search This Blog

Monday, September 17, 2012

NUnit AutoFixture unable to create an instance from IList

I encountered a problem with creating fake fixture objects in .NET tests for classes which contant a property with IList<T> (instead plain List<T>).

Suppose you have:

public class A
public IList<B> {
get { return list; }
set { list = value; }
private IList<B> list;

and you want to create a fake object like this:

var fake = new Fixture().Build<A>();

You'll get an error:
"AutoFixture was unable to create an instance from System.Collections.Generic.IList`1[A], most likely because it has no public constructor, is an abstract or non-public type."

What you need to do is tell AutoFixture what to use when it encounters an IList inferface:
(when there is an IList create a new List of some type):

var fixture = new Fixture();


If you like this post, please leave a comment :)