2

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=ASPNETDBEntities", "ASPNETDBEntities")
{
    this.OnContextCreated();
}

Any ideas? Thanks in advance.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

what if it's the scenario in the second paragraph, where the context is actually mocked, but you want to utilize attributes of a base object context in the mock object? Can you just put a mock connection string in the app.config?
@lorddev: I'm not quite clear on what kind of scenario you're trying to describe, but if you can't send in a "plain" mock object, configured to return whatever you expect it to when you interact with it, then you probably haven't achieved a clear separation of your layers - in other words, you should probably consider refactoring your code in order to make it easier to test.
I was hoping to access the ObjectStateManager in a disconnected Context mock. Turns out it's built in to EF and depends on an actual ObjectContext which can't be created without a valid EF connection string.
@lorddev: So you have a class A which depends on a Context that does stuff accessing the ObjectStateManager? In that case, mock the Context to return what you expect, and skip the rest entirely.
@lorddev: If I'm not understanding you correctly this time, maybe it's better to write up your own question about it, with some more details on what you're trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.