views:

69

answers:

3

Does anyone know how to test private functions in a Silverlight Unit Test project? The *_Accessor objects don’t seem to be available that are their in a normal unit testing project.

+4  A: 

You cannot unit-test private functions. You have 3 options:

  1. You can make those functions 'public' and test them,
  2. You make them 'internal' and add the InternalsVisibleTo attribute in the assembly file.
  3. You create a public or internal method that calls your private methods, and test those.

Unit testing is usually done to test the interface of classes to the outside world. Unit testing private methods is not recommended.

sbenderli
A: 

The answer by @sbenderli is correct.

But, I have my reservations about making private methods internal just to unit test them. Making a method internal is like making it public for that assembly.

Instead a better way would be to make the method protected and create a dummy class in your test assembly by inheriting from the class under test and then creating a public method which calls the protected method. Now you test the public method of the fake class.

P.K
The reflection approach doesn't work in Silverlight: http://msdn.microsoft.com/en-us/library/stfy7tfc(VS.95).aspx, that's why the _Accessor approach fails. I think the _Accessors in mstest are a flawed idea in any case...
OdeToCode
Thanks @OdeToCode. I will edit my answer.
P.K
A: 

If you have a genuine need to test private methods, then your architecture is in some way broken.

Duncan Bayne