views:

72

answers:

3

I created a standard ASP.NET MVC project and then added a SQL Express database in the App_Data folder. The connection string is recorded into the web.config file. I then created a LINQ to SQL data model for the database. I want to test the data model from my Test project but I don't know how to go about this correctly because I am using an attached database. Since the database is attached to the MVC project and not the Test project, how would I be able to access it from the Test project?

A: 

If you're talking about Unit Tests, they ought to be able to run without the database. The idea is that your code is broken into re-usable blocks that can be individually tested by passing in mocked / stubbed data.

Sohnee
Thx for ur quick answer! But does this mean that unit tests should not be used for testing ur data model using live data? I thought it would be a more accurate test if I use live data instead of fabricated data. LOL Maybe I did not understand the purpose of unit testing. But still I hope someone has done this before and let me know if it can be done.
anonymous
A: 

Sohnee is correct; a unit test should not have dependencies on your production database. It sounds like you're looking to do some broader integration testing, perhaps?

I could go into some detail on this, but I don't think it is what you're looking for. Could you elaborate on what you mean or aim to accomplish by "testing the data model?"

Jay
A: 

Some schools of thought are very strict on what should go into a unit test and not. Others not so much.

It's all about preferences, I think. Just remember that it's easy to forget to test the real thing when you mock everything out.

But to answer your question, what happens if you just create the data context in your test? Can you query against it, or do you get weird connection exceptions?

If the latter is the case, and you still want to test against your database, I guess you have to copy the database file to somewhere your test can reach it. Anyway, you can pass the connection string to the data context constructor.

Thomas Eyde