Stefano Tommesani

  • Increase font size
  • Default font size
  • Decrease font size
Home SDLC Necessary evil: testing private methods

Necessary evil: testing private methods

Some might say that testing private methods should be avoided because it means not testing the contract, that is the interface implemented by the class, but the internal implementation of the class itself. Still, not all classes were designed with testability in mind, so real life compromises sometimes demand such a trick.

When writing unit tests in C# with MSTest, the PrivateObject class lets you easily call private methods:

  1. [TestMethod]
  2. public void TestLPRead()
  3. {
  4.   var Logger = A.Fake<ILogger>();
  5.   var Telemetry = A.Fake<ITelemetry>();
  6.   DefaultDataModel DM = new DefaultDataModel(Logger, Telemetry);
  7.   PrivateObject obj = new PrivateObject(DM);
  8.   List<LPRead> ReadsList = (List<LPRead>)obj.Invoke("GetReads");

In the code above, a PrivateObject instance is created passing an instance of the class to be tested

  1. PrivateObject obj = new PrivateObject(DM);

then the invocation of the private method, that would be

  1. List<LPRead> ReadsList = DM.GetReads();

if the method were public, becomes

  1. List<LPRead> ReadsList = (List<LPRead>)obj.Invoke("GetReads");
Quote this article on your site

To create link towards this article on your website,
copy and paste the text below in your page.




Preview :


Powered by QuoteThis © 2008
Last Updated on Sunday, 29 January 2017 22:39  
View Stefano Tommesani's profile on LinkedIn

Latest Articles

Unit-testing file I/O 26 November 2017, 12.09 Testing
Unit-testing file I/O
Two good news: file I/O is unit-testable, and it is surprisingly easy to do. Let's see how it works! A software no-one asked for First, we need a piece of software that deals with files and that has to be unit-tested. The
Fixing Git pull errors in SourceTree 10 April 2017, 01.44 Software
Fixing Git pull errors in SourceTree
If you encounter the following error when pulling a repository in SourceTree: VirtualAlloc pointer is null, Win32 error 487 it is due to to the Cygwin system failing to allocate a 5 MB large chunk of memory for its heap at
Castle on the hill of crappy audio quality 19 March 2017, 01.53 Audio
Castle on the hill of crappy audio quality
As the yearly dynamic range day is close (March 31st), let's have a look at one of the biggest audio massacres of the year, Ed Sheeran's "Castle on the hill". First time I heard the song, I thought my headphones just got
Necessary evil: testing private methods 29 January 2017, 21.41 Testing
Necessary evil: testing private methods
Some might say that testing private methods should be avoided because it means not testing the contract, that is the interface implemented by the class, but the internal implementation of the class itself. Still, not all
I am right and you are wrong 28 December 2016, 14.23 Web
I am right and you are wrong
Have you ever convinced anyone that disagreed with you about a deeply held belief? Better yet, have you changed your mind lately on an important topic after discussing with someone else that did not share your point of

Translate