2010-07-21 81 views
1

我尝试进行我的第一个简单的数据驱动测试。使用DataSource属性进行单元测试

  1. 我使用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() 
{//...} 

但我得到这个错误:

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.

我有问题,如何附有SQL枭雄工作室创建数据库文件。有什么建议?

回答

1

here,你可以尝试:

  1. 更新您的连接字符串如下:

驱动程序= {SQL本机客户端};服务器= \的SQLExpress; AttachDbFilename = | DataDirectory目录| mydbfile.mdf;数据库= DBNAME; Trusted_Connection =是;

为什么需要数据库参数?如果已命名的数据库已连接,则SQL Server不会重新连接它。它使用附加的数据库作为连接的默认值。

  1. 在身份选项卡中的应用程序池属性下,将“网络服务”或“ASP.NET”设置为安全帐户。

  2. 请确保您授予对“网络服务”或“ASP.NET”帐户的读写权限。授予完全权限并稍后调整以安全(或不安全)地进行。