1

In most unit test tutorials I follow it has me create a second unit test project and then add a solution/project reference from the test project to the other project.

I have a VSTO addin, and when I go to add the reference I don't see anything listed under Solution > Project.

Why is this? How else can I add a reference to the addin project to test it?

forsvarir
  • 10,749
  • 6
  • 46
  • 77
ss7
  • 2,902
  • 7
  • 41
  • 90
  • Usually you'd add the test project to the solution containing the project you want to test. If for whatever reason you can't do that, you should be able to add a reference to the assembly containing the code by using the `browse` option on the add reference dialogue and navigating to the assembly. – forsvarir Jun 18 '15 at 06:05
  • It's added to same solution, but usually I add reference to it under solution > project. My project isn't there. So how do I reference it from my Uitt Test Project (inside the same solution) without adding that reference. Am I missing something every tutorial described doing it this way, it works when I follow them but my addin isn't listed. Is it because it's a VSTO addin? – ss7 Jun 18 '15 at 06:32
  • 1
    You can add test class and test members in the same VSTO project to test some of your methods. You would not be able to unit test all the methods in Addin until you mock excel. – Sarvesh Mishra Jun 18 '15 at 10:07
  • When using xUnit with an Outlook VSTO project, the AddIn project should show up under the "Projects" section of the Reference Manager window. See my question/answer on how I got xUnit working with my Outlook VSTO AddIn project in VS 2019 [here](https://stackoverflow.com/q/75739113/20036274). – wopr_xl Mar 15 '23 at 10:43

2 Answers2

4

If this is your first time at unit testing, then VSTO might be a steep learning curve. As suggested by @Sam Holder, you might want to put some of your logic into a separate assembly to encourage you to separate your logic from your interactions with the office infrastructure.

That said, whilst you can't add the project using the normal approach of adding a reference to the project, you can add a reference to the output from that project using the Browse option:

  • Right click on the test project and select "Add Reference".
  • In the Box the appears, click on the "Browse" button at the bottom.
  • Navigate to the bin\debug folder of the VSTO project and select the projects dll.
  • Click on OK.

Obviously, you will have had to have built the addin for the above to work.

You're going to face some other challenges if you start trying to create objects that rely on the office infrastructure. So, at a minimum you might need to add references to Microsoft.Office.Tools, Microsoft.Office.Tools.Common into your test project.

forsvarir
  • 10,749
  • 6
  • 46
  • 77
  • The folders debug and release are completely blank. Why would that be? And I can't add those references from my unit testing project – ss7 Jun 19 '15 at 06:42
  • 1
    @shenk Have you built the project? If so, check the output path for the project (right click on the project, select properties, select build, scroll down to build path). You can't add the references because the dll doesn't exist, or because of some other reason? – forsvarir Jun 19 '15 at 07:10
  • @forsavrir yes i can't add the reference because I can't find the DLL. Also the output path is default bin\debug – ss7 Jun 19 '15 at 07:13
  • 1
    @shenk Do a rebuild all and look in the build output window, it should show you the full path of the binary that's been created, or some kind of error. If it's saying that the binary is being created and it's not actually there, then something odd is going on. – forsvarir Jun 19 '15 at 07:16
  • Why can't I add Microsoft.Office.Tools to my Test Project? Just Microsoft Office.Interop.Outlook and Microsoft.Office.Core. The poster suggested tools and tools.common – ss7 Jun 19 '15 at 07:30
  • @shenk I don't know. I don't really do VSTO development. I just added a reference to the project I was trying to work with, built it and then started adding references to the assemblies that the build complained about. It might be down to the version of the framework you're using / version of visual studio... – forsvarir Jun 19 '15 at 07:43
  • Ahh I see, It's a pain to test these projects first time for me so I'm having a hard time. I don't see how I can substitute a fake activeinspector and such. I'm slowly getting the hang of it – ss7 Jun 19 '15 at 07:45
2

I don't know why adding a reference doesn't work, but you might be able to solve it by adding a 3rd project. Place all your logic in this new project. Add a reference to this new project in your tests, and then add a reference to you new project in your VSTO addin project, where you can call the logic.

Sam Holder
  • 32,535
  • 13
  • 101
  • 181