Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing TypeInitializationException error

I am working on a unit testing for a function in a form call Tester.cs, below is the error:

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:

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

What is the error? Thank you.

like image 539
Alvin Avatar asked Jul 31 '12 15:07

Alvin


2 Answers

The likelyhood is that ConfigurationManager.AppSettings["Part"] is returning null. Hense the parse exception.

Does your unit test project have the app setting defined in its configuration?

like image 67
Bob Vale Avatar answered Nov 05 '22 15:11

Bob Vale


You should copy your app.config (or web.config) file into test project. Otherwise test project can't find it. Remember config is related to host process, not to the dll itself.

like image 36
Sasha Avatar answered Nov 05 '22 15:11

Sasha