在单元测试项目连接字符串中引用数据库App_Data文件夹 [英] Connection string in unit test project to reference database in app_data folder

查看:424
本文介绍了在单元测试项目连接字符串中引用数据库App_Data文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待我引用数据库文件在我的单元测试项目。这是一个ASP.NET MVC应用程序。

I am looking to reference my database file in my unit test project. This is an ASP.NET MVC app.

请注意:我知道我不应该在我的单元测试可以访问数据库,但是这是在一个测试速战速决,我需要有通行证刚才

Please note: I know I should not be accessing the database in my unit tests but this is for a quick fix on one test that I need to have pass just now.

下一个里程碑后,我会嘲讽数据库访问方法等。

After the next milestone I will be mocking the database access methods etc.

因此​​,这里是我的MVC应用程序web配置和单元测试ap.config文件我的连接字符串

So here is my connection string in my mvc app web config and the unit test ap.config files

<add name="DBConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB.MDF;Integrated Security=True;User Instance=True"
  providerName="System.Data.SqlClient" />

当我运行测试我得到一个错误:

When I run the test I get an error:

Test method
ED.Tests.Controllers.CandidateControllerTest.PersonalDetailsStepPostShouldRedisplayIfNoSurnameSupplied 
threw exception:  System.Data.SqlClient.SqlException: 
An attempt to attach an auto-named database for file C:\Users\Desktop\ED\TestResults\LAPTOP-D 2009-07-22 18_16_20\Out\DB.MDF failed. 
A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

在我看来连接字符串是错误的,但我不知道如何正确设置路径。我曾尝试添加\\ .. \\ ..和目录名等。

It seems to me the connection string is wrong but I'm not sure how to set the path properly. I have tried adding \..\.. and the directory names etc.

推荐答案

MSTest的将在每个测试运行一个完全不同的文件夹中运行你的单元测试组件。这个想法是,每次运行从previous和后续运行一个完全隔离的情况下。它实际上是一种痛苦,告诉它的数据文件与应用程序的其他部分一起复制。你需要在你的解决方案(而不是你的项目),选择添加,创建一个新的测试运行配置右键单击。然后,你需要编辑测试运行配置,并指定哪些文件将被复制到测试执行的文件夹。应该有一个同级目录到解决方案目录中称为TestResults其中包含了用于每个测试运行的文件夹。

MSTest will run your unit test assembly in a completely different folder on every test run. The idea is that each run is a completely isolated case from previous and subsequent runs. It's actually kind of a pain to tell it to copy data files along with the rest of your application. You need to right-click on your solution (not your project), choose add, create a new test run configuration. Then you need to edit the test run configuration and specify which files will be copied to the test execution folder. There should be a sibling directory to your solution directory called TestResults which contains the folders used for each test run.

这篇关于在单元测试项目连接字符串中引用数据库App_Data文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆