单元测试TypeInitializationException错误

Alv*_*vin 4 c# unit-testing

我正在对表单调用Tester.cs中的功能进行单元测试,以下是错误:

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
Run Code Online (Sandbox Code Playgroud)

第35行是一个代码,该代码基本上从配置文件中检索值并将其转换为整数:

private static int _part = int.Parse(ConfigurationManager.AppSettings["Part"]);
Run Code Online (Sandbox Code Playgroud)

有什么错误?谢谢。

Bob*_*ale 5

可能ConfigurationManager.AppSettings["Part"]是返回null。解析解析异常。

您的单元测试项目的配置中是否定义了应用程序设置?


Sas*_*sha 5

您应该将app.config(或web.config)文件复制到测试项目中。否则,测试项目将找不到它。请记住,config与主机进程有关,而不与dll本身有关。