问题
I am looking to reference my database file in my unit test project. This is an ASP.NET MVC app.
Please note: I know I should not be accessing the database in my unit tests but this is for a quick fix on one test that I need to have pass just now.
After the next milestone I will be mocking the database access methods etc.
So here is my connection string in my mvc app web config and the unit test ap.config files
<add name="DBConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB.MDF;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
When I run the test I get an error:
Test method
ED.Tests.Controllers.CandidateControllerTest.PersonalDetailsStepPostShouldRedisplayIfNoSurnameSupplied
threw exception: System.Data.SqlClient.SqlException:
An attempt to attach an auto-named database for file C:\Users\Desktop\ED\TestResults\LAPTOP-D 2009-07-22 18_16_20\Out\DB.MDF failed.
A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
It seems to me the connection string is wrong but I'm not sure how to set the path properly. I have tried adding \..\.. and the directory names etc.
回答1:
MSTest will run your unit test assembly in a completely different folder on every test run. The idea is that each run is a completely isolated case from previous and subsequent runs. It's actually kind of a pain to tell it to copy data files along with the rest of your application. You need to right-click on your solution (not your project), choose add, create a new test run configuration. Then you need to edit the test run configuration and specify which files will be copied to the test execution folder. There should be a sibling directory to your solution directory called TestResults which contains the folders used for each test run.
回答2:
you can simply reference localdb like this:
<add name="DefaultConnection"
connectionString="Server=(localdb)\v11.0;Database=WebPortalDb" providerName="System.Data.SqlClient"/>
where WebPortalDb is your database name.
回答3:
Comment to Craig's answer: It shouldn't be a pain to deploy extra data files for unit test execution. You can use DeploymentItemAttribute (Microsoft.VisualStudio.TestTools.UnitTesting) at class or method levels to specify which files need to be copied prior to running those tests.
回答4:
The best answer is not to connect to a database at all with some Unit Tests, and instead mock the database. However, you may need to refactor existing MVC code to get to a point where you CAN mock a database dependency, and you should never refactor until your code is wrapped in some tests. So in that situation, yes - you'll probably need to connect your test project to the database, at least for a while until you decouple everything enough to enable mocking.
So in my instance I was using ASP.Net MVC 5. This meant I could get the necessary Entity Framework config added to the Unit Test Project's app.config file simply by Right-Clicking on the Solution file, Managing NuGet for Solution, and adding EF to the test project.
Then adding the connection string from the MVC project's main Web.config file NEARLY worked, with the minor tweak that I had to remove the final part of the string that said:
AttachDbFilename=|DataDirectory|<<MY_DATABASE_NAME_AND_ID>>.mdf
As I say, creating 'Unit' tests that connect to a database is not the way to go, so this will only be a temporary measure. But the code needs refactoring to get it to a mockable state, and (s)he who refactors without at least a few tests to prove the refactoring hasn't broken everything is cruising for a bruising.
来源:https://stackoverflow.com/questions/1166883/connection-string-in-unit-test-project-to-reference-database-in-app-data-folder