Search code examples
c#visual-studiounit-testingcomcom-interop

how to write a unit test for a Visual Studio plugin?


Some of the plugin's methods for which I'd like to write unit tests accept COM objects passed directly by Visual Studio. For example:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
   DTE2 d = (DTE2)application;
   ...
}

Because these objects are not serializable, I can't have unit test code read them from disk.

Other than creating instances of custom classes that implement the same COM-related interfaces and that have properties that return meaningless/dubious data, is there a way to test this can kind of method without actually launching Visual Studio?


Solution

  • I usually find harder to Unit Test these type of code. This is mainly because, as you said, hard dependencies with COM-related interfaces.

    You might be able to abstract away some of the dependencies, and test those types in isolation. See some samples see here.

    Personally I would not recommend writing Unit Tests against these types of methods. It is more of an entry point to the Plugin and it basically orchestrate calls. You probably don't get much value Unit Testing this method.

    Below are some that might help you in the direction of writing tests using DTE instance

    DTE and Unit Testing

    How to Unit Test Visual Studio AddIn interacting with VS DOM