1

I'm trying to add unit tests to my MVC application. Specifically, I'm currently trying to test parts of a Web API controller.

When my test method attempts to actually the load the controller, I got an error that indicated I had not defined the required database connection strings.

Even though the connection string are defined in my main project, I understand I needed to add them to my test project's app.config, which I did. But then I got a new error.

VegaModel.ssdl(2,2) : error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file.

I don't get this error, but the suggested fix seemed easy enough and I copied the <entityFramework> section from my web.config to my test project's app.config.

And now I get this error:

Unrecognized configuration section entityFramework. (C:\Users\Jonathan\Documents\Viper\Branches\Vega\Vega.Tests\bin\Debug\Vega.Tests.dll.config line 38)

Well, okay, those last two errors seems to contradict each other. But more importantly, my main program uses Entity Framework but my test project does not (yet anyway). So I'm troubled about this path that seems to lead towards getting more and more of Entity Framework configured in my test project.

What is the minimalist approach to being able to load a controller that uses Entity Framework from a unit test project?

5
  • 1
    Your entity framework section is invalid from the copy, why are you doing it manually, why don't you use nuget to install the proper version of EF in your unit test project, and let it add the configuration Commented Feb 28, 2017 at 21:00
  • 4
    Sounds like you need to look into mocking Commented Feb 28, 2017 at 21:00
  • @johnny5: I guess I didn't fully understand why I need Entity Framework in my test project. But I guess in this case the main app is not running and instead I'm just loading a class in my test application. So, I guess I'll try your suggestion. But that then makes me wonder what else I will need to install. Everything that's installed in my main app? Commented Feb 28, 2017 at 21:02
  • 3
    @JonathanWood Ideally you should be mocking your context access for unit test's otherwise your actually doing integration testing. If you've implemented a repository pattern you should be able to abstract the logic. Commented Feb 28, 2017 at 21:05
  • @I'm just using dbContext, which is part of it's own project. I am referencing that project from my test project. This seems like the best way for me. But the app uses other stuff. There are message filters that perform authentication. I either have to pull in all that stuff, or maybe it's just easy to create a separate application that makes the API calls and test it that way. Commented Feb 28, 2017 at 21:08

1 Answer 1

2

While this sounds more like an integration test than an unit test, that second error

Unrecognized configuration section entityFramework.

is because the following is missing from the top of the config file.

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <!--other sections removed for brevity -->
  <entityFramework>
    <!-- removed for brevity -->
  </entityFramework>
</configuration>

That aside, you indicated

So I'm troubled about this path that seems to lead towards getting more and more of Entity Framework configured in my test project.

One suggestion is to abstract away the tight coupling to Entity Framework so that Controller can be unit tested in isolation via mocking of these dependencies.

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

Comments

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.