【问题标题】:Web.config Configsource and file attributes not applied when unit testing单元测试时未应用 Web.config 配置源和文件属性
【发布时间】:2013-07-03 05:53:58
【问题描述】:

我的主项目中有一个 Appsettings 类型初始化程序,它从我的 web.config 文件中的 appSettings 获取字符串。这个 appSettings 部分和我的 connectionStrings 部分使用 configSource、文件等属性来引用值。

一切正常,除了当我的单元测试项目试图测试它时。

在 appSettings 类型初始化程序中引发错误,即 web.config 中的 appSettings/connectionStrings 值未定义。

如何让我的应用程序正确初始化,以便可以访问 web.config? 我需要初始化我的主项目(MVCApplication)的实例吗?

导致实体框架也没有连接字符串,导致 web.config 中未设置值

【问题讨论】:

  • 单元测试时,默认读取app.config
  • 我的情况有点复杂,它引用了/Configs/AppSettings.local.config

标签: c# asp.net-mvc unit-testing


【解决方案1】:

我建议将 App.config 添加到您的单元测试项目中,并在您的 app.config 中引用您的 /Configs/AppSettings.local.config。

<configuration>
   <appSettings file='/Configs/AppSettings.local.config' />
</configuration>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    相关资源
    最近更新 更多