开发者

Unit testing private functions in Silverlight

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 proje开发者_如何学Goct.


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.


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.


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


The open source framework Impromptu-Interface is able to expose private members using the DLR. The unit test for this feature are passing on Silverlight.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜