2010-07-21 25 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」帳戶的讀寫權限。授予完全權限並稍後調整以安全(或不安全)地進行。