开发者

Unit test with DataSource attribute

I try make my first simple Data Driven Test.

  1. I created db (UnitTestsDb) with MS SQL Management studio, and also I created one db table (UsersTab).

I try use this db in u开发者_JAVA百科nit 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?


From here, you can try:

  1. Update your connection string to the following:

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.

  1. Under the application pool properties in the identity tab, set "Network Service" or "ASP.NET" as the security account.

  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜