How are people unit testing code that uses Linq to SQL

LinqUnit TestingLinq to-Sql

Linq Problem Overview


How are people unit testing code that uses Linq to SQL?

Linq Solutions


Solution 1 - Linq

Update:

Fredrik has put an example solution on how to do unit test linq2sql applications over at his blog. You can download it at:

http://web.archive.org/web/20120415022448/http://iridescence.no/post/DataContext-Repository-Pattern-Example-Code.aspx

Not only do I think its great that he posted an example solution, he also managed to extract interfaces for all classes, which makes the design more decoupled.

My old post:

*I found these blogs that I think are a good start for making the DataContext wrapper: Link1 Link2

They cover almost the same topic except that the first one implements means for extracting interfaces for the tables as well. The second one is more extensive though, so I included it as well.*

Solution 2 - Linq

3 years late, but this is how I do it:

https://github.com/lukesampson/LinqToSQL-test-extensions/

No need to write a wrapper or do lots of plumbing, just drop the T4 template next to your .dbml and you get:

  1. An interface for your data context e.g. IExampleDataContext
  2. An in-memory mock for your data context e.g. MemoryExampleDataContext

Both will automatically use the mappings you've already configured in your DBML.

So you can do things like

public class ProductRepo {
    IExampleDataContext DB { get; set };
    public ProductRepo(IExampleDataContext db) {
        DB = db;
    }

    public List<Product> GetProducts() {
        return DB.Products.ToList();
    }
}

and you can call that with either

new ProductRepo(new MemoryExampleDataContext()).GetProducts(); // for testing

or

new ProductRepo(new ExampleDataContext()).GetProducts(); // use the real DB

Solution 3 - Linq

Wrap the DataContext, then mock the wrapper. It's the fastest way to get it done, although it requires coding for testing, which some people think smells. But sometimes, when you have dependencies that cannot be (easily) mocked, it's the only way.

Solution 4 - Linq

Normally, you don't need to test the part of the code that uses LINQ to SQL but if you really want to, you can use the same data sets that you're querying against the server and turn them into in-memory objects and run the LINQ queries against that (which would use the Enumerable methods instead of Queryable).

Another option is to use Matt Warren's mockable version of the DataContext.

You can also get the SQL statements that LINQ to SQL uses by getting them via the debugger (from the IQueryable object), check those manually, and then include them in the automated tests.

Solution 5 - Linq

Linq makes testing much easier. Linq queries work just as well on Lists as on the Linq-to-sql stuff. You can swap out Linq to SQL for list objects and test that way.

Solution 6 - Linq

Mattwar over at http://blogs.msdn.com/mattwar/archive/2008/05/04/mocks-nix-an-extensible-linq-to-sql-datacontext.aspx">The Wayward Web Log had a great article about how to mock up an extensible Linq2Sql data context. Check it out -- http://blogs.msdn.com/mattwar/archive/2008/05/04/mocks-nix-an-extensible-linq-to-sql-datacontext.aspx">MOCKS NIX - AN EXTENSIBLE LINQ TO SQL DATACONTEXT

Solution 7 - Linq

LINQ to SQL is actually really nice to unit test as it has the ability to create databases on the fly from what is defined in your DBML.

It makes it really nice to test a ORM layer by creating the DB through the DataContext and having it empty to begin with.

I cover it on my blog here: http://web.archive.org/web/20090526231317/http://www.aaron-powell.com/blog/may-2008/unit-testing-linq-to-sql.aspx

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeterView Question on Stackoverflow
Solution 1 - LinqPresidentenView Answer on Stackoverflow
Solution 2 - Linquser87453View Answer on Stackoverflow
Solution 3 - Linquser1228View Answer on Stackoverflow
Solution 4 - LinqMark CidadeView Answer on Stackoverflow
Solution 5 - LinqMendeltView Answer on Stackoverflow
Solution 6 - LinqDanimalView Answer on Stackoverflow
Solution 7 - LinqAaron PowellView Answer on Stackoverflow