Faking LINQ to SQL DataContext Tables with Isolator
A user on the Typemock Forums asked recently about faking the tables of a LINQ to SQL data context, and supplying fake test data instead. With Isolator, faking the DataContext object is easy, all it takes is just a few lines of code, without making any additional changes to the existing design.
Let’s consider the following scenario:
Let’s write a test for this method using Typemock Isolator!
LINQ to SQL uses the Table<TEntity> class to represent a table for a particular type in the database. We can supply fake values to this class using Isolator’s method WillReturnCollectionValuesOf(), which takes in an IEnumerable. However, in order to query this data via LINQ, the data must be convertible to IQueryable<T>. We can use AsQueryable() extension method to do that:
And that is it, our test passes, and no calls to the database have been made.