Failed to connect to DB during testing

I have an asp.net mvc application that uses the default user database. This all works very well, but I would like to create some tests for it. I have a test project, I immediately came across 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 works fine (at least in the mvc web.config project). Exception thrown by Entity DB Access Logic Designer

public ASPNETDBEntities() : 
            base("name=ASPNETDBEntities", "ASPNETDBEntities")
{
    this.OnContextCreated();
}

      

Any ideas? Thanks in advance.

+2


source to share


1 answer


The problem is that when you run your unit tests, you need to set a connection string in the App.config file of the test project 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 mock objects for testing. (If it's hard to do in your code, as it is, you may need to refactor your code ...)



The third possible scenario is that you are doing integration testing and therefore want to access a real db when testing, however it does not have to be a real db. It can be any db with the same database schema. I would recommend setting up a dummy db with some dummy entries in it that you can execute with tests (and which you put on a line in the App.config file in your test project) that won't grow and slow down when the "real" db does ...

+2


source







All Articles