How to check private variables in NUNit?

I have a static class that has a private variable.

private static Dictionary<strng, string> items = new Dictionary<string, string>();

Many public class methods access this vocabulary object. Now I want to write a NUNit testing class (in another library). How can I check this private variable?

+5
source share
4 answers

I know that this is about NUnit, and I do not want to argue about whether it is good or bad practice to test private members. The fact is that sometimes it is necessary, especially when you have to deal with outdated or poorly developed code that cannot be reorganized.

, Gallio/MbUnit API , .


: SomePrivateMethod foo.

[Test]
public void SampleTest()
{
   var foo = new Foo();
   int actual = Mirror.ForObject(foo)["SomePrivateMethod"].Invoke();
   Assert.AreEqual(123, actual);
}
+4

, . /// .. , InternalsVisibleToAttribute.

, MyUnitTestsLibrary, AssemblyInfo.cs (, ).

[assembly:InternalsVisibleTo("MyUnitTestsLibrary")]

MyUnitTestsLibrary , .

+8

, .

, .

. .

+3

, . , , , . , , .

+3

All Articles