0

I have an ASP.NET MVC project that I would like to unit test. Most of the functions rely on a call to our database, who's connection string is defined as:

connStr.ProviderConnectionString = Web.Configuration.WebConfigurationManager.ConnectionStrings("ApplicationServices").ConnectionString

Is there some way to easily test this without making each unit test fire off the dev server with:

 <TestMethod(), _
 HostType("ASP.NET"), _
 AspNetDevelopmentServerHost("C:\Users\tyler.HQ\Development\LateralSports\Dev\Dev\Site", "/"), _
 UrlToTest("http://localhost:57110/")> _

Using the HostType allows the unit tests to run, but it is super slow.

Is there some configuration parameter I can swap around?

Thanks

1 Answer 1

2

You really should find a better way to get at the connection string. Options include:

  • Use the normal System.Configuration.ConfigurationManager rather than whatever that thing is.
  • Use dependency injection to abstract it away.
  • Avoid the DB in unit tests entirely -- it is a bit of an infrastructure concern after all.
Sign up to request clarification or add additional context in comments.

5 Comments

I'm intrigued by your last point. How are we supposed to check if data is persisted correctly if we don't test the DB? I convert most of my Entity objects to custom types (I only need pieces of the Entity object) when showing it to the user. I need to make sure my custom types correctly get mapped back to Entity types. I'm new to this, so I'm sure I'm doing something strangely.
So confirm that they get mapped back correctly. Let the guys responsible for the EF make sure they write to the database correctly.
+1. See also the Repository Pattern.
Gotchya. We are a small shop. I'm now responsible for the EF as well. Somebody else did all the configuration, so I was really confused. I'll check out your ideas. Thanks!
Gotcha. By "guys responsible for the EF" I mean the ADO.NET team at Microsoft, not your small shop :)

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.