我想爲我的項目編寫單元測試,但它不會讓我使用配置管理器。現在我的項目的設置類似於不能在單元測試項目中使用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