How to: Regenerate Private Accessors
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.
Note |
|---|
| 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
-
Open the source-code file that contains the private method that has changed.
-
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.
Note 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
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.
- 8/19/2010
- Son of Magni
- 8/19/2010
- Son of Magni
- 8/27/2009
- Joel Estrellado
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.
- 5/15/2009
- Prakash Venkatesan
- 5/18/2009
- Thomas Lee
- 8/6/2008
- Dragthor