我想为我的项目编写单元测试,但它不会让我使用配置管理器。现在我的项目的设置类似于不能在单元测试项目中使用ConfigurationManager
ASP.Net应用程序(所有aspx页面)
ProjectCore(所有的C#文件 - 模型)
ProjectTest(所有测试)
在我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.ConnectionStrings [“的ConnectionString” ] .ConnectionString是我将数据适配器的配置字符串设置为,并在测试中返回空错误,但在实际使用网站时不会返回空错误。有任何想法吗?
你正在做单元测试和单元测试你的注意力应该是想测试特定的方法和应该删除多余的依赖。在这种情况下,尝试嘲笑/摩尔(使用MS Mole/Pex)system.configuration类;那肯定会给出一个解决方案。 – Rahul
@JohnSaunders所以在我的测试中,NullReferenceException是当我实际分配字符串到配置管理器。例如... String cs = ConfigurationManager.ConnectionStrings [“ConnectionString”]。ConnectionString – willykao
@JohnSaunders除非如果ConfigurationManager仅在单元测试的情况下为空(因为我在实际的ASP中没有得到这个错误。网络应用程序),想知道我是否真的可以在测试中使用配置管理器 – willykao