C# 无法在单元测试项目内使用ConfigurationManager

C# 无法在单元测试项目内使用ConfigurationManager,c#,sql,unit-testing,configuration,mocking,C#,Sql,Unit Testing,Configuration,Mocking,我正试图为我的项目编写一个单元测试,但它不允许我使用Configuration Manager。现在我的项目是这样设置的 ASP.Net应用程序(所有aspx页面) ProjectCore(所有C#文件-模型) 项目测试(所有测试) 在我的ProjectCore中,我可以从System.Configuration访问ConfigurationManager对象,并将信息传递到项目中。但是,当我运行涉及ConfigurationManager的测试时,我得到了错误 System.NullRefer

我正试图为我的项目编写一个单元测试,但它不允许我使用Configuration Manager。现在我的项目是这样设置的

ASP.Net应用程序(所有aspx页面)

ProjectCore(所有C#文件-模型)

项目测试(所有测试)

在我的ProjectCore中,我可以从System.Configuration访问ConfigurationManager对象,并将信息传递到项目中。但是,当我运行涉及ConfigurationManager的测试时,我得到了错误

System.NullReferenceException: Object reference not set to an instance of an object.
下面是一个测试示例

using System.Configuration;

[TestMethod]
public void TestDatabaseExists()
{
    //Error when I declare ConfigurationManager
    Assert.IsNotNull(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString
}

在我的其他测试中,ConfigurationManager.ConnectionString[“ConnectionString”]。ConnectionString是我将数据适配器的配置字符串设置为的,并在测试中返回空错误,但在我实际使用网站时不返回空错误。有什么想法吗?

这可能是几个问题之一:

  • 您没有将app.config添加到ProjectTest项目中
  • 您没有在app.config中添加连接字符串


  • 您正在进行单元测试,在单元测试中,您的浓度应该是尝试测试的特定方法,并且应该删除无关的依赖项。在这种情况下,尝试模仿/模仿(使用Microsoft Mole和Pex)
    system.configuration
    class;这肯定会给出一个解决方案

    我要说的是,一旦您安装了MS moles和pex->在您的测试项目解决方案中->右键单击系统组件并选择创建mole


    这将为您提供一个摩尔版本的配置类,而这个配置类又将有一个模拟版本的
    配置类
    ——使用它您可以绕过您所面临的问题

    它与mstest.exe命令行中的/noisolation参数有关。
    省略/noiseolation参数,它就可以工作。

    您还可以在
    ExeConfigurationFileMap
    中使用特殊的配置路径:

    // Get the machine.config file.
    ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
    // You may want to map to your own exe.config file here.
    fileMap.ExeConfigFilename = @"C:\test\ConfigurationManager.exe.config";
    // You can add here LocalUserConfigFilename, MachineConfigFilename and RoamingUserConfigFilename, too
    System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
    

    首先,必须确保nunit测试项目中有app.config文件

    要添加它,可以打开项目属性(右键单击项目)

    输入连接的详细信息,它将生成app.config文件或在以下内容中添加正确的部分:

    在测试类中,将引用添加到:System.Configuration; =>使用系统配置

    例如,您可以通过以下方式使用connectionString:

    [TestFixture]
    public class CommandesDALUnitTest
    {
    
        private string _connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    
        [Test]
        public void Method_Test()
        {
            string test = _connectionString;
                ....
        }
    }
    

    您正在进行单元测试,在单元测试中,您的浓度应该是尝试测试的特定方法,并且应该删除无关的依赖项。在这种情况下,尝试模拟/模拟(使用MS Mole/Pex)system.configuration类;这肯定会给出一个解决方案。@johnsa在我的测试中理解,NullReferenceException是指当我实际将字符串分配给配置管理器时。例如字符串cs=ConfigurationManager.ConnectionString[“ConnectionString”]。ConnectionString@JohnSaunders除非ConfigurationManager仅在单元测试中为null(因为我在实际的ASP.net应用程序中没有收到此错误),想知道我是否可以在中实际使用configurationmanagertests@Rahul我在这附近的另一个解决方案中看到,我将看一看,
    ConfigurationManager.ConnectionString
    为null,或者
    ConfigurationManager.ConnectionString[“ConnectionString”]
    为null。哈哈哈,哎呀,我有一个web.config文件而不是app.config。原因是我在ASP.net项目中使用了web.config,而且由于core不需要它在那里工作的配置文件,我想我可以使用相同的配置文件,但这不是问题,谢谢提醒。这实际上是一个评论,不是答案。一旦你获得了足够的声誉,。我就不得不使用这个解决方案,因为我的集成测试项目中的配置文件没有加载。它是一个带有xUnit测试和app.config设置的类库。我正在Visual Studio 2017中使用ReSharper。我可以在bin文件夹中看到app.config被正确重命名,并且我可以看到项目正在该文件夹中运行。还没有找到任何其他解决方案。