app.configs和MSTest的项目 - 连接字符串空引用字符串、项目、app、configs

2023-09-03 03:38:42 作者:*мaη懷情事 ̄|

当我试图运行单元测试(MSTest的),我遇到了这个问题。 的code线:

When I try to run Unit Tests (mstest) I run into this issue. The line of code:

_mainCnStr = System.Configuration.ConfigurationManager.
               ConnectionStrings["main"].ConnectionString;

回来为空引用

Comes back as a null reference

这当我运行它并没有做到这一点,在主界面的项目。什么是正确的方法来获取该连接字符串设定看到的单元测试项目?我试图嵌入的资源。我想一直拷贝。什么是设置的最佳组合,这将解决这个问题对我?

It doesn't do this in the main UI project when I run it. What is the right method to get this connection string setting seen by the Unit Test project? I tried embedded as a resource. I tried Copy Always. What is the right combination of settings that will fix this for me?

推荐答案

您应该添加的app.config 的单元测试项目。它不会自动使用在设置UI应用程序的的app.config

You should add an app.config to the unit test project. It won't automatically use the settings in UI application's app.config.