简体   繁体   English

单元测试TypeInitializationException错误

[英]Unit testing TypeInitializationException error

I am working on a unit testing for a function in a form call Tester.cs, below is the error: 我正在对表单调用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

On line 35 is a code, which basically retrieve value from configuration file and convert it to integer: 第35行是一个代码,它基本上从配置文件中检索值并将其转换为整数:

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

What is the error? 错误是什么? Thank you. 谢谢。

The likelyhood is that ConfigurationManager.AppSettings["Part"] is returning null. 可能性是ConfigurationManager.AppSettings["Part"]返回null。 Hense the parse exception. 强制解析异常。

Does your unit test project have the app setting defined in its configuration? 您的单元测试项目是否在其配置中定义了应用程序设置?

You should copy your app.config (or web.config) file into test project. 您应该将app.config(或web.config)文件复制到测试项目中。 Otherwise test project can't find it. 否则测试项目找不到它。 Remember config is related to host process, not to the dll itself. 记住config与主机进程有关,而不是与dll本身有关。

Put the break point in line 35, something there is null which should not be...Null value can not be parsed into INT.... 将断点放在第35行,其中有一个null,不应该是......空值无法解析为INT ....

Be ensure that the config file is placed correctly at the directory from which you're running the application 确保将配置文件正确放置在运行应用程序的目录中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM