2012-07-31 102 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 ....

是确保配置文件是在正确放置从中运行应用程序的目录