I came across this issue recently when trying to create unit tests with Microsoft Visual Studio 2008 SP1. Test stubs were created for the Public methods of the class but VS2008 could not create a Private Accessor for the Private methods e.g.:
///
<summary>
///A test for TrimTimeOffDate
///</summary>
[TestMethod()]
[DeploymentItem("WardyIT.Services.Hermes.Plugins.dll")]
public
void TrimTimeOffDateTest()
{
// Creation of the private accessor for 'Microsoft.VisualStudio.TestTools.TypesAndSymbols.Assembly' failed
Assert.Inconclusive("Creation of the private accessor for \'Microsoft.VisualStudio.TestTools.TypesAndSy" +
"mbols.Assembly\' failed");
}
After Googling I came across a site that talked about running Visual Studio as Administrator on Microsoft Vista. To do this I right clicked on the Visual Studio 2008 menu item and selected "Run as administrator". Once I did this the Private Accessors were successfully created e.g.:
///
<summary>
///A test for TrimTimeOffDate
///</summary>
[TestMethod()]
[DeploymentItem("WardyIT.Services.Hermes.Plugins.dll")]
public
void TrimTimeOffDateTest()
{
SchedulingEngine_Accessor target = new
SchedulingEngine_Accessor(); // TODO: Initialize to an appropriate value
DateTime DateToTrim = new
DateTime(); // TODO: Initialize to an appropriate value
DateTime expected = new
DateTime(); // TODO: Initialize to an appropriate value
DateTime actual;
actual = target.TrimTimeOffDate(DateToTrim);
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
Other suggestions were:
- It's bad practice to test Private Methods. Link
- It's bad practice to have too many classes because you have split out Private Methods into new classes. Link
- To use the PrivateObject Class if you can't use the Private Accessor. Link
Other Links:
A Unit Testing Walkthrough with Visual Studio Team Test