5 out of 12 rated this helpful - Rate this topic

How to: Regenerate Private Accessors

Visual Studio 2005

When you generate a unit test for a private method, a private accessor is automatically created. A private accessor is a method that the test method uses to access the private code.

When you change a private method in your code-under-test file, you might have to regenerate the test's private accessors, as described in the following procedure.

NoteNote

After you regenerate private accessors, subsequent runs of the unit test for private methods might fail; in particular, you might see compile-time failures in your test project. This occurs because, although the private accessors are now up to date, unit test methods are still dependent on the previous definitions of the private accessors and must now be updated. To fix this error, regenerate the unit test; see How to: Generate a Unit Test. The new unit test, which will now correctly reference the newly regenerated private accessor, is appended to the end of the test source-code file. If you have worked in the original unit test, such as by assigning values in lines that contain // TODO: comments, you will want to copy the code you changed from the original unit test into the new version of the test.

To regenerate private accessors

  1. Open the source-code file that contains the private method that has changed.

  2. Right-click in this source-code file, point to Create Private Accessor, and select the test project into which to place the updated private accessors file.

    To the VSCodeGenAccessors file, this adds a new accessor class that contains methods for retrieving values of the entities in the private class being tested.

    NoteNote

    Do not edit the VSCodeGenAccessors file by hand.

    This creates up-to-date private accessors for all the private methods in your code-under-test file, whether private accessors for them existed previously.

See Also

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
When "Create Private Accessor" does not appear in the context menu
"Create Private Accessor" never appears in the context menu, so this procedure is not possible to use.  It seems that the only way to regenerate the private accessors is to create a new unit test project and copy the private accessors file from there to your existing unit test project.

Follow up:
You won't see "create private accessor" unless your unit test project is currently in your solution.  If you don't keep it as part of the solution you need to "add existing project" then regenerate the accessors, then remove from the solution.
This took a while to fix too, but here's how to fix it
Make sure you are out of VS2008. Then in your test project directory, delete everything below bin and obj directories. Go back to VS2008 and load the solution, then build all.
Useless - This never works

I have a method for which I already created unit test. I refactored the method to include a new parameter. I am unable to regenerate the VSCodeGenAccessors.cs. I tried whatever is described here. I am using VS2008.

Just watch your PrivateObject constructor.
Just watch your PrivateObject constructor. One parameter takes a system type... and if the type you incorrectly pass in has an indentical method (and signature)... the test will keep failing. For example, you want to test ClassA.Foo() however you incorrectly passed in ClassB (that also has the same Foo signature) in the PrivateObject constructor. A stupid little mistake like this can drive you crazy. (I am using Visual Studio 2008 Professional).