<p>How are people unit testing code that uses Linq to SQL? </p>

[{'answer_id': 52671, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 2, 'selected': False, 'text': '<p>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).</p>\n\n<p>Another option is to use Matt Warren\'s <a href="http://blogs.msdn.com/mattwar/archive/2008/05/04/mocks-nix-an-extensible-linq-to-sql-datacontext.aspx" rel="nofollow noreferrer">mockable version of the DataContext</a>.</p>\n\n<p>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.</p>\n'}, {'answer_id': 52678, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 52757, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 52884, 'author': 'Danimal', 'author_id': 2757, 'author_profile': 'https://Stackoverflow.com/users/2757', 'pm_score': 2, 'selected': False, 'text': '<p>Mattwar over at <a href="http://blogs.msdn.com/mattwar/archive/2008/05/04/mocks-nix-an-extensible-linq-to-sql-datacontext.aspx" rel="nofollow noreferrer">The Wayward Web Log</a> had a great article about how to mock up an extensible Linq2Sql data context. Check it out -- <a href="http://blogs.msdn.com/mattwar/archive/2008/05/04/mocks-nix-an-extensible-linq-to-sql-datacontext.aspx" rel="nofollow noreferrer">MOCKS NIX - AN EXTENSIBLE LINQ TO SQL DATACONTEXT</a></p>\n'}, {'answer_id': 125571, 'author': 'Presidenten', 'author_id': 15763, 'author_profile': 'https://Stackoverflow.com/users/15763', 'pm_score': 5, 'selected': True, 'text': '<p>Update:</p>\n\n<p>Fredrik has put an example solution on how to do unit test linq2sql applications over at his blog. You can download it at:</p>\n\n<p><a href="http://web.archive.org/web/20120415022448/http://iridescence.no/post/DataContext-Repository-Pattern-Example-Code.aspx" rel="nofollow noreferrer">http://web.archive.org/web/20120415022448/http://iridescence.no/post/DataContext-Repository-Pattern-Example-Code.aspx</a></p>\n\n<p>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.</p>\n\n<p>My old post:</p>\n\n<p>*I found these blogs that I think are a good start for making the DataContext wrapper:\n<a href="http://web.archive.org/web/20120414060200/http://iridescence.no/post/Linq-to-Sql-Programming-Against-an-Interface-and-the-Repository-Pattern.aspx" rel="nofollow noreferrer">Link1</a>\n<a href="http://nixusg.com/post/2008/08/05/The-Automated-Testing-Continuum-Part-2-(Unit-Testing-LinQ).aspx" rel="nofollow noreferrer">Link2</a></p>\n\n<p>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.*</p>\n'}, {'answer_id': 171842, 'author': 'Aaron Powell', 'author_id': 11388, 'author_profile': 'https://Stackoverflow.com/users/11388', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n\n<p>It makes it really nice to test a ORM layer by creating the DB through the DataContext and having it empty to begin with.</p>\n\n<p>I cover it on my blog here: <a href="http://web.archive.org/web/20090526231317/http://www.aaron-powell.com/blog/may-2008/unit-testing-linq-to-sql.aspx" rel="nofollow noreferrer">http://web.archive.org/web/20090526231317/http://www.aaron-powell.com/blog/may-2008/unit-testing-linq-to-sql.aspx</a></p>\n'}, {'answer_id': 9353934, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>3 years late, but this is how I do it:</p>\n\n<p><a href="https://github.com/lukesampson/LinqToSQL-test-extensions/" rel="noreferrer">https://github.com/lukesampson/LinqToSQL-test-extensions/</a></p>\n\n<p>No need to write a wrapper or do lots of plumbing, just drop the T4 template next to your .dbml and you get:</p>\n\n<ol>\n<li>An interface for your data context e.g. IExampleDataContext</li>\n<li>An in-memory mock for your data context e.g. MemoryExampleDataContext</li>\n</ol>\n\n<p>Both will automatically use the mappings you\'ve already configured in your DBML.</p>\n\n<p>So you can do things like</p>\n\n<pre><code>public class ProductRepo {\n    IExampleDataContext DB { get; set };\n    public ProductRepo(IExampleDataContext db) {\n        DB = db;\n    }\n\n    public List&lt;Product&gt; GetProducts() {\n        return DB.Products.ToList();\n    }\n}\n</code></pre>\n\n<p>and you can call that with either</p>\n\n<pre><code>new ProductRepo(new MemoryExampleDataContext()).GetProducts(); // for testing\n</code></pre>\n\n<p>or</p>\n\n<pre><code>new ProductRepo(new ExampleDataContext()).GetProducts(); // use the real DB\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/52657', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5189/']