DB connection failed while testing
I have an asp.net mvc application, which uses the default user database. It all works pretty well, but I would like to create some tests for it. I Have a test project, I immediately stumble upon an exception
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
But the connection string is working perfectly (at least in the mvc project's web.config). The exception is thrown by my Entity DB access logic constructor
public ASPNETDBEntities() :
base("name=ASPNETDBE开发者_开发技巧ntities", "ASPNETDBEntities")
{
this.OnContextCreated();
}
Any ideas? Thanks in advance.
The problem is that when running the unit tests, you need to have a connection string set in the App.config file of the test project in order for Entity Framework to find it.
However, if you're doing unit testing you most likely don't want to access the db at all, but rather mock up some dummy objects to test against. (If this is hard to do in your code as it is, you might need some refactoring of your code...)
A third possible scenario is that you're doing integration testing, and thus want to access a real db when testing - however, it doesn't have to be the real db. It can be any db with the same database schema. I'd recommend setting up a dummy db with some dummy records in it, which you can perform tests against (and which you put a connectionstring for in the App.config file in the test project) that will not grow and become slower when the "real" db does.
精彩评论