2012-07-31 106 views
3

我上的單元測試工作的形式呼叫Tester.cs的函數,下面是錯誤:單元測試TypeInitializationException錯誤

Error 7/31/2012 10:43:11 PM One of the background threads threw exception: 
System.TypeInitializationException: The type initializer for 'Tester.Tester' threw an exception. ---> System.ArgumentNullException: Value cannot be null. 
Parameter name: String 
    at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) 
    at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) 
    at System.Int32.Parse(String s) 
    at Tester.Tester..cctor() in E:\Incubator\Tester\Tester\Tester.cs:line 35 
    --- End of inner exception stack trace --- 
    at Tester.Tester.Dispose(Boolean disposing) 
    at System.ComponentModel.Component.Finalize() MY-PC 

在線路35是一個代碼,它基本上由結構檢索值文件,並將其轉換爲整數:

private static int _part = int.Parse(ConfigurationManager.AppSettings["Part"]); 

什麼錯誤?謝謝。

回答

4

可能性是ConfigurationManager.AppSettings["Part"]返回null。強化解析異常。

您的單元測試項目是否具有在其配置中定義的應用程序設置?

+0

不,可能是tq的原因。 – Alvin 2012-07-31 16:01:00

3

您應該將您的app.config(或web.config)文件複製到測試項目中。否則測試項目找不到它。記住配置與主機進程有關,而不是dll本身。

+0

哦,是的,app.config必須粘貼... – Alvin 2012-07-31 16:00:30

0

把破發點,第35行,東西有空這不應該是...空值不能被解析成INT ....

是確保配置文件是在正確放置從中運行應用程序的目錄