Skip to content
April 26, 2008 / Bob Black

Unit Test Shadowing

I looked carefully at the unit test VS.Net 2008 had just generated. Looked standard enough, except… what was this? A class named CodeFragment_Accessor?

PrivateObject param0 = null;

CodeFragment_Accessor target = new

int expected = 0;

int actual;

target.TextLength = expected;

actual = target.TextLength;

Assert.AreEqual(expected, actual);

Assert.Inconclusive(“Verify the correctness of this test method.”);


Hold on now! That wasn’t one of my classes – I’d never seen it before, although I did in fact have a class called CodeFragment. I right-clicked and selected Go To Definition from the popup menu.


CodeFragment_Accessor : BaseShadow




The class looked familiar somehow. On closer inspection, it appeared to be a copy of my CodeFragment class, except that in this _Accessor version, all the fields I had declared private were declared public. Hmmm.

I cracked open the MSDN documentation and ran a search for the ShadowingAttribute. MSDN displayed the helpful hint: Do not use this class.

Really. That’s what it said. Do not use this class. Lighten up Microsoft, I wasn’t going to use it, I just wanted to know what it’s for! Sheesh. And that’s pretty much all the documentation it looked like I was going to get on the infamous ShadowingAttribute.

But moving right along – the _Accessor class inherits from a class called BaseShadow. Maybe that would provide some clues.

And I was right – MSDN reports that BaseShadow
is used by the publicize method to help you test private types.

Well now, that makes a whole lot more sense, and is actually pretty snazzy too. Although, to be honest, I’m not sure what publicize method they’re referring to. I mean, I see what they’re getting at, but that seems like odd language. Did they mean there’s a class somewhere that has a method named publicize, which uses the BaseShadow class in some way, or does this refer, in a general sense, to the process of publicizing a test? Or something.

Anyway, VS.Net’s unit test framework apparently uses some fancywork (using reflection) to ensure complete code coverage (instead of generating tests for only public fields, properties, and methods). You’re not going to get much help from Google doing a search for ShadowingAttribute, but take it from me that you can (and should) safely use the generated _Accessor class to unit test both public and private accessors in your class. And this is another great reason why you should let VS.Net stub out your unit tests for you instead of building them from scratch yourself – hand coding the “shadow wrapper” would take a while.



Leave a Comment
  1. Side Holidays / Aug 9 2010 8:02 pm

    This is a nice one. I was digging about it and got only “Do not use this class”. Thanks for the explanation.

  2. Chris / Sep 7 2010 6:54 am

    Very cool, finally I got what this damn Accessor is all about. Cheers mate!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: