views:

2131

answers:

6

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

A: 

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.

Mark Cidade
Why may someone not need to test the part of the code that uses LINQ to SQL? I'm e.g. writing an asp.net mvc web page where many controllers use LINQ to SQL and I have to test these controllers...
Thomas Danecker
I meant that you don't need to make sure that LINQ to SQL itself works as advertised (since Microsoft already tested that for you) as opposed to your code that uses any results from the LINQ to SQL classes.
Mark Cidade
+4  A: 

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.

Mendelt
+3  A: 

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

Will
+2  A: 

Mattwar over at The Wayward Web Log had a great article about how to mock up an extensible Linq2Sql data context. Check it out -- MOCKS NIX - AN EXTENSIBLE LINQ TO SQL DATACONTEXT

Danimal
+7  A: 

Update 2:

Made sure the link below works.

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://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: http://iridescence.no/post/Linq-to-Sql-Programming-Against-an-Interface-and-the-Repository-Pattern.aspx http://nixusg.com/post/2008/08/05/The-Automated-Testing-Continuum-Part-2-(Unit-Testing-LinQ).aspx 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.

Presidenten
The actual address on the first link is incorrect, copy and paste the address to see the really useful article.
Colin Desmond
+1  A: 

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://www.aaron-powell.com/blog.aspx?id=1125

Slace