CSharp

Unit Testing Private Static Methods in C#

I had never needed to unit test a private static class method in C# before, but it turns out that Microsoft created a special object type to handle just such a case.

To test the private static method DetermineFilename (that takes filePath as a string parameter and returns a string) in the FileManager class, do the following:

PrivateType privateFileManagerObject = new PrivateType(typeof(FileManager));
string filename = (string)privateFileManagerObject.InvokeStatic("DetermineFilename", filePath);

Thanks to Venkat for posting this solution on his web site here:
http://venkatcsharpinterview.blogspot.com/2011/07/unit-testing-private-static-method-in-c.html

Disable C# “var” Recommendation in ReSharper

I have been trying to use type names instead of “var” in my C# code lately.  However, ReSharper wants me to convert every type name in a declaration to “var”.  The squiggles under my type names were driving me nuts, so I decided to dig into the settings to turn this off.

To disable these suggestions, do the following:

  1. Go to the ReSharper menu and select Options…
  2. From the resulting dialog box, select Code Inspection, Inspection Severity.
  3. Select the C# tab and then Language Usage Opportunities.
  4. Set both “Use ‘var’ keyword when initializer explicitly declares type” and “Use ‘var’ keyword when possible” to “Do not show”.

Rob

Go to Top