views:

57

answers:

2

Is there any guideline on how to use Visual Studio 2008 unit test development? I am developing a n-tier web application using entity framework. My problem is to create Unit Test for the methods which INSERT or UPDATE to the database.

+1  A: 

Do not create unit tests to test if you are INSERTING or DELETING or doing anything to the database. There simply is no need. If you are using ADO.NET or an ORM for example, this code will have been tested. You'll know that it actually does what it says on the tin.

Testing data being stored in a database is not a unit test. A good question on what makes a unit test, a unit test. For your code that is using your database code (DAL, ORM etc...) you will insert mock objects (test doubles) that will allow the rest of your application to believe it is interacting with a database, when in fact it's not.

If you wish to ensure your code is actually taking data and storing it on a DB or external file you'll need to write integration tests to check this. One method that is possible is to use a test database that is set up at each test fixture with dummy data, you perform your tests then wipe the database back to its prior state. The benefit of this is you will have unit tests for your business logic, and intergration tests to ensure you've wired the database code up correctly.

Finglas
A: 

This isn't really about the tools you are using, but more about the design.

The solution to your problem is to redesign your code to be able to use stub or mock objects.

Another option is to make sure all test code is done inside a new transaction.

If you ever start working with EF 4.0 you can benefit of the IObjectSet class.

Gerrie Schenck