具有 DataSource 属性的单元测试
我尝试进行第一个简单的数据驱动测试。
- 我使用 MS SQL Management studio 创建了数据库 (UnitTestsDb),并且还创建了一个数据库表 (UsersTab)。
我尝试在单元测试中使用这个数据库。代码在这里:
[TestMethod()]
[TestProperty("TestCategory","Developer"),
DataSource("System.Data.SqlClient",
"Data Source=.\\SQLEXPRESS;AttachDbFilename=UnitTestsDb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True",
"UsersTab", DataAccessMethod.Sequential)]
public void UserConstructorTest()
{//...}
但我收到此错误:
单元测试适配器无法连接到数据源或读取数据。有关解决此错误的详细信息,请参阅“数据驱动单元测试疑难解答”(http://go.microsoft.com/fwlink/?LinkId=62412" rel="nofollow noreferrer">http:// /go.microsoft.com/fwlink/?LinkId=62412)位于 MSDN 库中。
错误详细信息:尝试为文件 UnitTestsDb.mdf 附加自动命名数据库失败。存在同名数据库,或无法打开指定文件,或位于 UNC 共享上。
我有问题,如何使用 SQL Managment studio 创建附加数据库文件。有什么建议吗?
I try make my first simple Data Driven Test.
- I created db (UnitTestsDb) with MS SQL Management studio, and also I created one db table (UsersTab).
I try use this db in unit test. Code is here:
[TestMethod()]
[TestProperty("TestCategory","Developer"),
DataSource("System.Data.SqlClient",
"Data Source=.\\SQLEXPRESS;AttachDbFilename=UnitTestsDb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True",
"UsersTab", DataAccessMethod.Sequential)]
public void UserConstructorTest()
{//...}
But I get this error:
The unit test adapter failed to connect to the data source or to read the data. For more information on troubleshooting this error, see "Troubleshooting Data-Driven Unit Tests" (http://go.microsoft.com/fwlink/?LinkId=62412) in the MSDN Library.
Error details: An attempt to attach an auto-named database for file UnitTestsDb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
I have problem, how attached db file created with SQL Managment studio. Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从此处,您可以尝试:
驱动程序={SQL Native Client};服务器=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;数据库=数据库名称;Trusted_Connection=是;
为什么需要数据库参数?如果指定的数据库已附加,SQL Server 不会重新附加它。它使用附加数据库作为连接的默认值。
在“身份”选项卡的应用程序池属性下,将“网络服务”或“ASP.NET”设置为安全帐户。
在“身份”选项
确保您向“网络服务”或“ASP.NET”帐户授予读写权限。授予完全权限并稍后进行调整以确保安全(或不安全)。
From here, you can try:
Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
Why is the Database parameter needed? If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection.
Under the application pool properties in the identity tab, set "Network Service" or "ASP.NET" as the security account.
Make sure you grant read and write permissions to "Network Service" or "ASP.NET" account. Give full permissions and adjust later to be on the safe (or unsafe) side.