Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnitTesting Static Classes

Tags:

Scenario. Language C#, Unit testing using VS2008 Unit testing framework

I have a static class with a static constructor and 2 methods. I have 4 test methods written to test the entire class. My Static Constructor has some important initializations.

Now if I run all the 4 unit test cases in tandem, the static constructor will be called only at the beginning. At the end of each test case, there is no such thing

called static destructor, So the state info in the constructor gets carried to the next unit test case also. What is the workaround for this.

like image 484
SNA Avatar asked Jun 10 '09 03:06

SNA


1 Answers

Type staticType = typeof(StaticClassName);
ConstructorInfo ci = staticType.TypeInitializer;
object[] parameters = new object[0];
ci.Invoke(null, parameters);

from http://colinmackay.scot/2007/06/16/unit-testing-a-static-class/

like image 85
rob Avatar answered Oct 14 '22 05:10

rob