Unit Tests and the PrivateObject, Part 1

As a developer, it’s hard to stick to writing unit tests.  It’s really awesome if you do test driven development and write them first, but a lot of the times I find it hard to sick to this plan for various reasons.  No, it’s not cause I don’t want to….sort of.

One of the really cool things about the unit test framework built into visual studio, is the ability to create a unit test that will test even your private methods.  You can do this by right clicking in the .cs and selecting the “Create Private Accessor” option, which will then offer you the list of test projects to generate this class in.  Here is the menu:

  image

You can read more about doing this here.  Now, we should be able create an instance of you new accessor in the unit test by instantiating MyObject_Accessor.  In the instance of the object, you should now have access to all of the fields and methods of the original object that are not public.  Sweet.

Hanging off of this accessor object, you will see a property called Target, of type Object.  This holds something called a PrivateObject, which should be a wrapper around your original object, MyObject.  I have been doing a ton of Dependency Injection in my projects lately using Unity to decouple my projects from eachother.  Using the PrivateObject is critical to creating a mock version of my dependency injector for the purposes of unit testing.

In a follow up to this, I will post some code showing how to create a mock injector, and how to make use of the PrivateObject in that injector.  Now go test!

Share this post!

8 comments:

ether said...

A unit test is supposed to be testing the public surface of an object. If you have reach into its private parts, I'd call that a problem with the design.

Joe Seymour said...

so, no unit tests for private methods?

ether said...

Yeah, pretty much.. If your functionality in the private methods is so complex or has so many side-effects that you don't think that testing the classes surface area covers them adequately, then the private methods are already violating the Single Responsibility Principle and probably should become a separate dependency for your class and those be tested as that separate class.

Moez Mousavi said...

would you be kind and post some example code? I am doing unit testing with VS2008 and yes, I am testing private methods and I am using Accessor.
Public void method is populating a private field. I can not get the values in my test code...

Kaj Bonfils said...

I agree strongly with Ester. But if you have a brownfield project with lots of long methods violating Single Responsibility, it can be a strong and sometimes necessary way to test both existing and new functionality. I think the ideal scenario would be rewriting the method but often thats not an option

cocinas exclusivas said...

It suppose to provides a rich set of assertions to make your tests clean and readable...

car hire madrid said...

Nice article! Thank for sharing!

GoldyVPN said...

Once you get netflix vpn service you will experience high speed so you can easily test your gaming skills. You will be able to watch netflix from abroad from anywhere in the world.
http://www.goldyvpn.com/vpn-for-netflix/

Post a Comment