Stefano Tommesani

  • Increase font size
  • Default font size
  • Decrease font size
Home Programming 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

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
How Commercial Insight changes R&D 06 November 2016, 01.21 Web
How Commercial Insight changes R&D
The CEB's Commercial Insight is based on three pillars: Be credible/relevant – Demonstrate an understanding of the customer’s world, substantiating claims with real-world evidence. Be frame-breaking – Disrupt the

Translate