简体   繁体   中英

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 project.

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.

如果您确实需要测试私有方法,那么您的体系结构将在某种程度上被破坏。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM