ASP.NET 애플리케이션을 단위 테스트 할 때 web.config를 사용하는 방법
단위 테스트로 시작하고 있으며 연결 문자열에 web.config를 사용하는 방법이 있습니다.
나는 사용할 수 있기를 바랐다
[DeploymentItem("web.config")]
웹 구성 파일을 얻으려면 여전히 null 참조 예외가 남습니다 (다음 테스트를 작성하는 것입니다).
테스트하려는 프로젝트에 포함 된 구성 파일을 어떻게 사용합니까?
차이점이 있다면 VS 2008에 포함 된 테스트 프레임 워크를 사용하고 있습니다.
감사
단위 테스트 프로젝트에는 자체 구성 파일이 있어야합니다.
테스트 프로젝트에서 추가, 새 항목, 애플리케이션 구성 파일을 선택할 수 있습니다.
이 파일은 web.config와 똑같이 작동하지만 단위 테스트를 위해 작동합니다.
.NET을 사용하여 모든 위치에서 web.config 또는 app.config에로드 할 수 있습니다 OpenMappedExeConfiguration. System.Configuration프로젝트의 참조에가 추가 되었는지 확인하십시오 .
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap()
fileMap.ExeConfigFilename = @"c:\my-web-app-location\web.config"
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
string connectionString = config.AppSettings.Settings["ConnectionString"].Value;
다음은 꽤 표준 인 web.config입니다.
<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<appSettings>
<add key="ConnectionString" value="Data Source=XXXX;Initial Catalog=XXX; Trusted_Connection=True;"/>
</appSettings>
</configuration>
2017-09-29에 업데이트
파일에서 AppSetitngs를 더 쉽게 읽을 수 있도록 클래스를 만들었습니다. Zp Bappi 에서 아이디어를 얻었습니다 .
public interface IAppSettings
{
string this[string key] { get; }
}
public class AppSettingsFromFile : IAppSettings
{
readonly Configuration Config;
public AppSettingsFromFile(string path)
{
var fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = path;
Config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
}
public string this[string key]
{
get
{
return Config.AppSettings.Settings[key].Value;
}
}
}
수업을 사용하는 방법은 다음과 같습니다.
IAppSettings AppSettings = new AppSettingsFromFile(@"c:\my-web-app-location\web.confg");
string connectionString = AppSettings["ConnectionString"];
web.config 파일을 "/ bin"폴더에 복사하고 "AppName.dll.config"로 이름을 바꿉니다.
여기서 "AppName"-결과 어셈블리의 이름입니다.
이 해킹을 여러 번 사용했습니다.
You will want your results to be well-defined and repeatable. To to this, you'll need to be working against known data so that you can clearly define both your normal cases and your boundary cases. In my work, this is always a specific server and dataset so the Unit testing module has the connection string built in. Others prefer using a connection string out of the Unit Testing project. I've never seen anyone recommend the use of the web site's config file! (development or otherwise)
I would recommend abstracting the configuration reading part, so that it could be mocked. Something like this, see Jon_Lindeheim's reply How to read Web.Config file while Unit Test Case Debugging?
If you need a connection string, you are not writing a unit test (assuming that you are using the connection string for going to database). Unit tests are not supposed to interact with outside environment. You will want to run all of them after each check in so they better run at the speed of light.
For a unit test, you will want to isolate your code from your database. Modify your tests (and the code you are testing if necessary) so that you will not need to go to database for testing them.
'Nice programing' 카테고리의 다른 글
| Swagger 사양 JSON을 HTML 문서로 변환 (0) | 2020.11.01 |
|---|---|
| Electron (Atom Shell) 애플리케이션에서 사용자 설정을 저장할 위치는? (0) | 2020.11.01 |
| 왜 (0) | 2020.11.01 |
| 세마포어-초기 카운트의 사용은 무엇입니까? (0) | 2020.11.01 |
| 실행 가능한 jar 파일을 만드는 방법은 무엇입니까? (0) | 2020.11.01 |