【发布时间】:2009-07-22 17:25:17
【问题描述】:
我希望在我的单元测试项目中引用我的数据库文件。这是一个 ASP.NET MVC 应用程序。
请注意:我知道我不应该在我的单元测试中访问数据库,但这是为了快速修复我需要通过的一个测试。
在下一个里程碑之后,我将模拟数据库访问方法等。
这是我的 mvc 应用程序 web 配置和单元测试 ap.config 文件中的连接字符串
<add name="DBConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB.MDF;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
当我运行测试时出现错误:
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.
在我看来,连接字符串是错误的,但我不确定如何正确设置路径。我尝试添加 \..\.. 和目录名称等。
【问题讨论】:
-
这是VS2008自带的默认所以我觉得是的。
标签: asp.net-mvc unit-testing connection-string