具有 DataSource 属性的单元测试

发布于 2024-09-11 05:51:39 字数 947 浏览 4 评论 0原文

我尝试进行第一个简单的数据驱动测试。

  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()
{//...}

但我收到此错误:

单元测试适配器无法连接到数据源或读取数据。有关解决此错误的详细信息,请参阅“数据驱动单元测试疑难解答”(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.

  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍暮颜 2024-09-18 05:51:40

此处,您可以尝试:

  1. 将连接字符串更新为以下内容:

驱动程序={SQL Native Client};服务器=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;数据库=数据库名称;Trusted_Connection=是;

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

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

    在“身份”选项

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文