rickgaribay.net

Space shuttles aren't built for rocket scientists, they're built for astronauts. The goal isn't the ship, its the moon.
posts - 258, comments - 163, trackbacks - 28

My Links

News

Where's Rick?



Visual Studio Connections, 3/26

That Conference, 8/13



About Me
I am a developer, architect, writer and speaker, passionate about distributed .NET technologies and Application Lifecycle Management. I am currently the General Manager of the Connected Systems Practice at Neudesic.


    follow me on Twitter



    Archives

    Post Categories

    Published Works

    Unit Testing Private Methods

    We had a discussion today around how to test private methods in unit tests. I could have sworn I blogged about this about a year ago but I guess not.

    Anyway, let's say you have Implementation.dll that contains a private method. Within the AssemblyInfo.cs file for that assembly, simply add the InternalsVisibleTo attribute, naming the assembly that will be making the calls into the implementation.

    For example, if MyUnitTests.dll will call private methods on a class in Implementation.dll, add the following to the AssemblyInfo.cs file for Implementation.dll:

    [assembly: InternalsVisibleTo("MyUnitTestAssembly.dll")]

    Scary? Yes, but also very useful if you are truly applying TDD.

    More info here: http://msdn2.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx

     

    Print | posted on Wednesday, August 01, 2007 2:59 PM | Filed Under [ Processs ]

    Feedback

    Gravatar

    # re: Unit Testing Private Methods

    Does this work with private methods? I thought in only worked with internal ones.
    9/10/2007 7:46 PM | Greg Arzoomanian
    Gravatar

    # re: Unit Testing Private Methods

    This *really* isn't a good, not to mention, safe, way to design code. Instead, use VS' built-in ability to create reflective interfaces to private methods using the "Create Private Accessor" tool.
    9/17/2007 10:55 AM | Nathan Smith
    Gravatar

    # re: Unit Testing Private Methods

    Sorry Nathan but the "private accessor" may seem like a siimple and straightforward way of testing a non-public class/method, but it just creates to much unneccessary code on large systems. I've been in this IT programming/archtecture gig for nearly 30 years (OOD/OOP since 91) and the InternalsVisibleTo directive is a god send for developing and maintaining test harnesses and doing TDD on the MS platform. Regards :)
    6/19/2009 2:14 PM | bill in san diego
    Comments have been closed on this topic.

    Powered by: