A Problem Unit Testing with .NET Private Accessors

Recently I ran into a problem using .NET accessor methods to unit test private methods. I was able to resolve the problem, but in doing so ended up abandoning accessor methods in favor of a simpler approach (using reflection).

But should you even test private methods?

It's a nuanced question. On one hand, it seems reasonable to break a class into small, purposeful private methods (exposing the main points of the class publicly) but still test the smallest possible chunks of functionality by testing those private methods in addition to the public ones. On the other, testing only public methods should exercise those private methods enough, and testing private methods can make your test code more brittle to change. There's a good table here with more arguing points.

For my purposes, I decided that I should.

What's a private accessor method?

But how do you get at privates? The default .NET unit test suite (MSTest) provides a seemingly handy facility within Visual Studio to easily do this by generating so called "private accessors". 

Just right click on a private method in your code like so

then click "Create Private Accessor" and point it to a project containing your test code. and the tool magically provides proxy types (with the suffix "_Accessor") that expose these private methods publicly and that you can use from within your unit tests to access these private members like this:

var myObject = new MyClass_Accessor();
var result = myObject.PrivateMethod();

How useful, right?

A problem

I ran into trouble when I had a private method that returned a generic list of private classes. When I generated an accessor for the method, an accessor for the private class was also generated so that the accessor method actually returned a list of "MyPrivateClass_Accessor" types instead of "MyPrivateClass" types.

This generated code actually gives an error, and seems to be a bug in Visual Studio 2008. MyPrivateClass_Accessor derives from MyPrivateClass, and while you can cast a derived class to its base class, in .NET you can't cast a list of derived classes to a list of its base classes (as I assume it's doing internally in the generated code).

I suppose they're working to fix this.

A simpler alternative

Using a bit of code (good samples are available here and here) that uses reflection and extension methods, you can still fairly easily test these methods like so:

var myObject = new MyClass();
var result = (List<MyPrivateClass>) myObject.CallMethod("PrivateMethod");

Sure, there's extra casting work and you have to use nasty string literals for the method names, but this is test code after all, and if it breaks you should know quickly from a regular test run.

A lesson

One of the hazards, it seems, working with Microsoft tools is the strong focus on tooling itself. This can sometimes get in the way of more elegant solutions based on simpler principles.

Not that all or most of the tools are bad (a lot of them I feel make me more productive), but it's a glamorous trap that you should be aware of.

Comments

November 16. 2008 23:45

Busby SEO Test

tnx for the information about "A Problem Unit Testing with .NET Private Accessors"

Busby SEO Test

March 5. 2009 17:32

Perfect Home Design

yeah you made a choice for that

Perfect Home Design

March 18. 2009 19:21

Internet Marketing Company

Search Value is a highly professional & technically focused Internet Marketing Company.

Internet Marketing Company

May 26. 2009 09:13

tukang nggame

thanks for sharing the post it help me in configure and use the ASP.Net

tukang nggame

June 16. 2009 07:51

newreil

The article is usefull for me. I’ll be coming back to your blog.

newreil

June 26. 2009 11:36

SEO


Useful article.
Thanks for helpful information you catch up us with your instructional explanation.

SEO

June 26. 2009 18:34

cheap kamagra UK

nice will bookmark this

cheap kamagra UK

July 3. 2009 14:45

club penguin cheats

MyPrivateClass_Accessor derives from MyPrivateClass, and while you can cast a derived class to its base class, in .NET you can't cast a list of derived classes to a list of its base classes as I assume it's doing internally in the generated code.

club penguin cheats

July 17. 2009 18:21

Web Marketing

Excellently written article, if only all bloggers offered the same content as you, the internet would be a much better place. Please keep it up!

Web Marketing

July 23. 2009 01:01

Solicitor Prepared Home Information Pack

It is indeed a great resource to obtain information on this subject. Keep posting. Thanks.

Solicitor Prepared Home Information Pack

July 30. 2009 08:23

Website design   

The content on this site is unique. A good work done related to tutorials and must be carried on.

Website design   

August 4. 2009 22:17

emo hair

Excellently written article, if only all bloggers offered the same content as you, the internet would be a much better place. Please keep it up!
emo hair

emo hair

August 10. 2009 03:51

Seth Flowers

What about using the generic IEnumerable extension method Cast, or the generic List method ConvertAll.

Seth Flowers

August 13. 2009 02:33

Internet marketing

I must say great website. I have just googled it nice info out there.

Internet marketing

August 17. 2009 08:30

spyware removal

Hi,
Sometimes a user wants to save the content of an infected file because viruses can sometimes embed themselves in files, called code injection, and the file may be essential to normal operation. To do this, antivirus software will attempt to repair the file. To do this, the software will try to remove the viral code from the file. Unfortunately, some viruses might damage the file upon injection

spyware removal

August 17. 2009 08:31

Antivirus software

Hi,
A file repair operation fails, usually the best thing to do is to just delete the file. Deleting the file is necessary if the entire file is infected.[citation needed] This may be necessary in the case of infected ZIP files, or similar "packed" files.

Antivirus software

August 22. 2009 16:42

Web design

Nice article......I really like this source code presentation.....thank you for sharing with us....

Web design

August 24. 2009 07:24

college auto insurance

I must say great website. I have just googled it nice info out there.

college auto insurance

September 7. 2009 00:53

free poker

I too facing the same problem and searching for the solution. The testing only public methods should exercise those private methods enough, and testing private methods can make your test code more brittle to change.
Regards,

free poker

September 8. 2009 03:15

Custom Essay

I can't be able to get a perfect solution for this. As I am a new learner of this .NET. But I was facing this problem since from a week. I need the perfect solution. Thanks for sharing.
Regards,

Custom Essay

September 11. 2009 20:44

no deposit online casino bonus

The file may be essential to normal operation. To do this, antivirus software will attempt to repair the file. To do this, the software will try to remove the viral code from the file. Unfortunately, some viruses might damage the file upon injection.
regards,

no deposit online casino bonus

September 20. 2009 19:22

asus laptops

Can u compare jsp and asp.net over security issues, fastness, cost effective? Which is best to avoid hacking?

asus laptops

September 20. 2009 21:51

income protection

How to condect the database testing? Is there any procedure to contact the database tesing ?

income protection

September 21. 2009 22:34

Dating Site

It was really a nice tutorial! Thanks for sharing this information to us. Just hoping that this will not be the last post that I could be read written by you.

Dating Site

September 22. 2009 08:27

online poker

Thanks for sharing the post it help me in configure and use the ASP.Net

online poker

September 23. 2009 13:33

car hifi

well, this is a good post..A file repair operation fails, usually the best thing to do is to just delete the file. Deleting the file is necessary if the entire file is infected.[citation needed] This may be necessary in the case of infected ZIP files, or similar "packed" files.
regards,
tiny

car hifi

September 24. 2009 01:32

green tea and weight loss

hey,i red article about this it was pretty good..The testing only public methods should exercise those private methods enough, and testing private methods can make your test code more brittle to change.
regards,

green tea and weight loss

September 25. 2009 23:59

Tiffany Bracelets

hey,i red article about this it was pretty good..The testing only public methods should exercise those private methods enough, and testing private methods can make your test code more brittle to change.
regards,

Tiffany Bracelets

September 28. 2009 15:44

TimeshareDEPOT

Wow, I have to say - its always great hearing thought leadership of another bloggers like yourself, I thought this post was very well written. Thankyou for taking the time to write it.

TimeshareDEPOT

September 29. 2009 22:00

ugg classic boots

When one ugg classic cardy loves one ugg classic mini art no service seems too hard .Don gild the lily. I can live for two months on a good ugg boots on sale compliment.The decline of literature indicates the decline of a nation ; the two cheap ugg australia keep in their ugg bailey button downward tendency. http://www.uggshow.co.uk/

ugg classic boots