score:14
Update:
Fredrik has put an example solution on how to do unit test linq2sql applications over at his blog. You can download it at:
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.*
score:1
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
score:2
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.
score:2
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.
score:2
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
score:4
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.
score:5
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:
- An interface for your data context e.g. IExampleDataContext
- 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
Source: stackoverflow.com
Related Query
- How are people unit testing code that uses Linq to SQL
- How to return anonymous type from c# method that uses LINQ to SQL
- linq - how do you do a query for items in one query source that are not in another one?
- How to code the partial extensions that Linq to SQL autogenerates?
- How to return query results from method that uses LINQ to SQL
- How to write SQL translateable linq code that groups by one property and returns distinct list
- How to replicate in LINQ a SQL query that uses Coalesce to pivot rows into a single column
- How can you see the sql that is causing an error on SubmitChanges in LINQ to SQL?
- Linq to SQL - Ignore search parameters that are null or zero
- How can I switch that code to use LINQ
- How does this linq code that splits a sequence work?
- How do I use Linq to find the elements of a list that are not present in another list?
- How do i prevent Linq from adding a GUID so that SQL server can do it?
- How to get SQL query into LINQ form in C# code
- How to work around the fact that Linq to SQL is using separate SQL DELETE statements for each item in DeleteAllOnSubmit
- How do I do a linq query to find fields in a dataset that are present in every record of a set?
- How do i convert this linq code to inline sql
- How to retrieve one table row and list of rows that are connected to the first row in Linq .NET
- Linq to SQL Expression Properties That are Translatable to SQL
- how to be sure in Linq to SQL that record has been added successfully or not?
- Is there a resource that explains how the LINQ to objects operators are implemented?
- Linq to sql query: how to prevent duplication of code
- How to write Linq (or lambda) statement to produce all element that are NOT IN (or NOT EXIST)
- Updating LINQ to SQL but the query i have is from a join? Can't update using query that uses a join?
- How do I write a LINQ to SQL statement with a custom function that needs the compare the current item context to a passed in value?
- How to filter entities that are deleted using linq to entities
- How can I see the SQL text that is sent by LINQ to my database?
- Convert code that uses Linq to code that does not
- How can I make a LINQ Select that includes a child object like a SQL outer join?
- How to refactor a MongoDB .NET LINQ Query that uses a static method in a select statement
More Query from same tag
- Stored Procedure & LINQ, Dmbl File unable to interpret the result set
- using linq with Join and Max using DataTables
- LINQ expression to compare nested arrays
- UWP - XDocument Equivalent Of XMLNodeList
- LINQ select property by name
- Which is the better approach for filtering?
- When querying a collection using linq it always returns a null
- C# Refactoring gigantic switch statement for ordering with LINQ
- LINQ query filters not returning correct number of results
- Query a JSON using LINQ
- Filtering include items in LINQ and Entity Framework
- How to rollback changes to WPF DataGrid control using LINQ-to-SQL?
- Best method for programmatically requesting db table columns using LINQ
- How can I convert two methods into a single method to increase efficiency?
- anonymous type in vb.net
- Get Attribute Values using LINQ to XML with Unknown Elements
- Update Rows in the database with LINQ
- how to return/work/process a LINQ-Query List result?
- Formatting LINQ Query Result Set
- XML reading repeating child elements with same name as parent c#
- Cant use Linq WHERE/IN/JOIN with Entity Framework on many to many
- When is it bad form to return a deferred IEnumerable<T>
- Comparing Arrays using LINQ in C#
- Projection: filling 2 arrays at once
- How to find specific number from array?
- How can I have an index value inside an inner select in a LINQ expression?
- select for dynamic result
- LINQ - Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator
- How can I do this loop to print values across (x,y) axes from an IEnumerable?
- How to Select then OrderBy using LINQ?