score:14

Accepted answer

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.*

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:

  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

Related Query

More Query from same tag