Michael O'Dea-Jones' Blog

What to do when Visual Studio can’t create Private Accessors in your Unit Tests

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:

  1. It's bad practice to test Private Methods. Link
  2. It's bad practice to have too many classes because you have split out Private Methods into new classes. Link
  3. 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

Published Wednesday, December 03, 2008 10:47 AM by michael@wardyit.com

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Jeremy said:

Perfect!  I've been looking for this all week.

October 9, 2009 5:30 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit