in Education by
Has anyone got any suggestions for unit testing a Managed Application Add-In for Office? I'm using NUnit but I had the same issues with MSTest. The problem is that there is a .NET assembly loaded inside the Office application (in my case, Word) and I need a reference to that instance of the .NET assembly. I can't just instantiate the object because it wouldn't then have an instance of Word to do things to. Now, I can use the Application.COMAddIns("Name of addin").Object interface to get a reference, but that gets me a COM object that is returned through the RequestComAddInAutomationService. My solution so far is that for that object to have proxy methods for every method in the real .NET object that I want to test (all set under condition

1 Answer

0 votes
by
This is how I resolved it. Just about everything in my add-in runs from the Click method of a button in the UI. I have changed all those Click methods to consist only of a simple, parameterless call. I then created a new file (Partial Class) called EntryPoint that had lots of very short Friend Subs, each of which was usually one or two calls to parameterised worker functions, so that all the Click methods just called into this file. So, for example, there's a function that opens a standard document and calls a "save as" into our DMS. The function takes a parameter of which document to open, and there are a couple of dozen standard documents that we use. So I have Private Sub btnMemo_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles btnMemo.Click DocMemo() End Sub in the ThisAddin and then Friend Sub DocMemo() OpenDocByNumber("Prec", 8862, 1) End Sub in my new EntryPoints file. I add a new AddInUtilities file which has Public Interface IAddInUtilities #If DEBUG Then Sub DocMemo() #End If End Interface Public Class AddInUtilities Implements IAddInUtilities Private Addin as ThisAddIn #If DEBUG Then Public Sub DocMemo() Implements IAddInUtilities.DocMemo Addin.DocMemo() End Sub #End If Friend Sub New(ByRef theAddin as ThisAddIn) Addin=theAddin End Sub End Class I go to the ThisAddIn file and add in Private utilities As AddInUtilities Protected Overrides Function RequestComAddInAutomationService() As Object If utilities Is Nothing Then utilities = New AddInUtilities(Me) End If Return utilities End Function And now it's possible to test the DocMemo() function in EntryPoints using NUnit, something like this: Public Class Numbering Private appWord As Word.Application Private objMacros As Object Public Sub LaunchWord() appWord = New Word.Application appWord.Visible = True Dim AddIn As COMAddIn = Nothing Dim AddInUtilities As IAddInUtilities For Each tempAddin As COMAddIn In appWord.COMAddIns If tempAddin.Description = "CobbettsMacrosVsto" Then AddIn = tempAddin End If Next AddInUtilities = AddIn.Object objMacros = AddInUtilities.TestObject End Sub Public Sub DocMemo() objMacros.DocMemo() End Sub Public Sub TearDown() appWord.Quit(False) End Sub End Class The only thing you can't then unit test are the actual Click events, because you're calling into EntryPoints in a different way, ie through the RequestComAddInAutomationService interface rather than through the event handlers. But it works!

Related questions

0 votes
    Has anyone got any suggestions for unit testing a Managed Application Add-In for Office? I'm using NUnit ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    How to add a page in a started word document in Microsoft Office Word Select the correct answer from above options...
asked Dec 5, 2021 in Education by JackTerrance
0 votes
    How to add a page in a started word document in Microsoft Office Word Select the correct answer from above options...
asked Nov 26, 2021 in Education by JackTerrance
0 votes
    My If Else statement in VB.net is randomly displaying either of the If or ElseIf condition that I made ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    My If Else statement in VB.net is randomly displaying either of the If or ElseIf condition that I made ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    In VB.NET is there a library of template dialogs I can use? It's easy to create a custom dialog ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 30, 2022 in Education by JackTerrance
0 votes
    In VB.NET is there a library of template dialogs I can use? It's easy to create a custom dialog ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 30, 2022 in Education by JackTerrance
0 votes
    In VB.NET is there a library of template dialogs I can use? It's easy to create a custom dialog ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    In VB.NET is there a library of template dialogs I can use? It's easy to create a custom dialog ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    In VB.NET is there a library of template dialogs I can use? It's easy to create a custom dialog ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I have a VB6 program that someone recently helped me convert to VB.NET In the program, when saving ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I have a VB6 program that someone recently helped me convert to VB.NET In the program, when saving ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I don't seem to be able to close the OledbDataReader object after reading data from it. Here is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I don't seem to be able to close the OledbDataReader object after reading data from it. Here is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I don't seem to be able to close the OledbDataReader object after reading data from it. Here is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
...