0

I've to write unit tests for VSTO Plugin code. So, in my plugin, there is the main add-in class, ThisAddIn which has following declaration:

namespace NS
{
    public partial class ThisAddIn
    {  private void ThisAddIn_Startup(object sender, System.EventArgs e){...}
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e){...}
    }
}

Now, I can see in IDE that this is how it's being initialised in ThisAddIn.Designer.cs class:

 [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
        public ThisAddIn(global::Microsoft.Office.Tools.Outlook.Factory factory, global::System.IServiceProvider serviceProvider) : 
                base(factory, serviceProvider, "AddIn", "ThisAddIn") {
            Globals.Factory = factory;
        }

I referred this post: How to create constructor of ThisAddIn class in VSTO But could not take find something which will help me. Reason of explicitly creating this class's object is for writing unit tests for VSTO plugin. How can I create these input parameters and pass into the constructor call? Also, is this the right way to write unit test for the VSTO plugin?

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Akanksha_p
  • 916
  • 12
  • 20

2 Answers2

0

You can't unless you recreate all the plumbing for hosting VSTO addins (See IManagedAddin interface on MSDN - https://learn.microsoft.com/en-us/visualstudio/vsto/imanagedaddin-interface?view=vs-2022), but that is a lot of work just for unit tests.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • That's bit of a work. Instead, I'll create a new interface/class and put all the functionality in that and just use in the add-in class. – Akanksha_p Jun 25 '22 at 12:06
  • I would imagine you'd need to test various methods that use Application and other OOM objects. You won't be able to do that outside of the addin. – Dmitry Streblechenko Jun 26 '22 at 00:58
  • Yes, i guess. I think otherwise code which doesn't need Outlook interactions will cover more than 50% of codebase and that should suffice the code coverage condition. Also, can't restructure the codebase again just for unit tests. – Akanksha_p Jun 26 '22 at 07:02
0

You can mock up everything you need in unit tests. ThisAddin is just an implementation of the IDTExtensibility2 interface which all COM add-ins should implement. You may consider creating and implementing your own interface which can be tested and mocked up for the add-in class.

ThisAddin class can't be created by others. Instead, the VSTO runtime creates an instance of this class.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45