我正在尝试为我的项目编写单元测试,但它不会让我使用配置管理器.现在我的项目设置像
ASP.Net应用程序(所有aspx页面)
ProjectCore(所有C#文件 - 模型)
ProjectTest(所有测试)
在我的ProjectCore中,我能够从System.Configuration访问ConfigurationManager对象并将信息传递到项目中.但是,当我运行涉及ConfigurationManager的测试时,我收到错误
System.NullReferenceException: Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)
这是测试的一个例子
using System.Configuration;
[TestMethod]
public void TestDatabaseExists()
{
//Error when I declare ConfigurationManager
Assert.IsNotNull(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString
}
Run Code Online (Sandbox Code Playgroud)
在我的其他测试中,ConfigurationManager.ConnectionStrings ["ConnectionString"].ConnectionString是我将数据适配器的配置字符串设置为,并在测试时返回null错误,但在我实际使用网站时却没有.有任何想法吗?
我正在将 .netframework 4.7.2 类库迁移到 .netstandard 2.0 库。
ConfigurationManager 在.netframework 类库中用于读取应用程序设置。
.netstanard 中的 System.Configuration.ConfigurationManager 可用的任何替代选项。
迁移的类库需要在 .net core Web API 和旧的 .netframework Web 应用程序中使用。