views:

1072

answers:

3

I'm working on a project that uses MSTest for unit testing and I need to write some unit tests for a model class thats fairly tightly coupled to the data source. We maintain the source code for the database that the model classes are dependent on in a Database Project (.dbproj). What I was thinking of doing was writing a ClassInitialize method on my test class that uses the Database Project to programatically build a SQL CE instance of our database that I could then populate with data that I could connect my model class to and perform tests against it with known data.

I'm open to other suggestions if anyone has them.

What I'm trying to figure out how to do is run this Database Project from within code in my ClassInitialize method. I haven't been able to find any good examples of how to do something like that. Is this possible? If so, how? Anyone have a code sample? Thanks.

A: 

Perhaps you should look into mocking - moq is a good place to start. Basically mocking is having a framework that will impersonate dependencies in your code to aid in testing.

Andrew Hare
+1  A: 

Unfortunately, unit testing the data layer is notoriously difficult. The best I can suggest is that you build a generic interface for your data layer and mock that for your business layer calls into the data layer.

If you try to unit test your data layer you're going to be in for a world of pain. I suggest you skip it and try to flush out any data layer bugs during integration testing.

That said, if you're using LINQ to SQL, I ran across this article the other day that shows how to mock a LINQ to SQL repository. It requires some extra work to get it to work but it might be useful to you.

Randolpho
A: 

I agree with the comments about mocking, but you can still do automated integration testing to exercise the methods in your repository like classes.

I did a very detailed description of a technique I used (w/ MS Test) here

Toran Billups