A FakeDbSet that implements IDbSet and uses IList for data manipulation

A FakeDbSet that implements IDbSet and uses IList for data manipulation

If you have ever tried to test code that depends on DbSet or IDbSet by creating a fake that implements IDbSet, you may have found it more complicated than you planned, due to the fact that there are a lot of methods needed to implement the IDbSet interface to just get a minimal amount of functionality from your fake.

The big secret about this, in my opinion, is that most of the complexity comes from the IQueryable interface. Faking the IDbSet specific methods is a manageable goal if we find an existing object that implements the IQueryable interface for us.

Luckily, we can create a List object (which implements IEnumerable) and use the AsQueryable() to get an IQueryable that can execute queries using our List as the data source.

I have created a working FakeDbSet that accepts a passed-in List object and implements the IDbSet interface. It takes advantage of the IQueryable from AsQueryable and includes a wrapper around the QueryProvider to observe the CreateQuery and Execute activities.

Improve Your Life
Improve Your Team
Improve Your Code
Software Projects

David Walker

David Walker is a Secure Software Consultant, a Certified Secure Software Lifecycle Professional (CSSLP), and a Professional Scrum Master. He believes in secure and reliable software and productive happy teams. He lives in Orlando with his lovely wife Lynn and his 2 dogs.