Moq & Interop Types: works in VS2012, fails in VS2010?

Edit : It works for me when I try it in Visual Studio 2012 and target .Net 4.0, only using the .Net PIA’s not the COM ref. Same solution doesn’t work in VS2010.

VS2010 loads version’s 10.0.30319.1 of the Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll's and VS2012 loads version’s 11.0.50727.1. You can see the different version’s in the Modules window.


I managed to get it working in VS2010:

enter image description here

Here is my solution http://temp-share.com/show/Pf3Ypip62 for everyone’s convenience. It has all the Moq references included. I have Excel 2007 (ie v12) – so please adjust references to Office 14.

The Project with methods to be Tested has to use the PIA Microsoft.Office.Interop.Excel via the .Net reference tab.

In the Unit Test Project you have to use the Microsoft Excel 1X.0 Object Library via the COM reference tab – its an ActiveX.

The confusing thing is in Solution Explorer they are both called: Microsoft.Office.Interop.Excel

There is one other caveat that I dont know how to workaround – you have to use the .Net 3.5 framework and I was actually hoping Microsoft fixed it in 2012 as you found because I cant work how to do it with ALL projects in .Net 4.0. Some solutions with mixed projects targeting .Net 3.5 & 4.0 are ok.

I’ve had a lot of trouble with this, see here How do I avoid using dynamic when mocking an Excel.worksheet? and also see this question I asked: Mocked object doesn’t have all properties shown in Intellisense – in one project but has them in the other.

Anyway this is how to get it working in VS 2010. I’m glad its resolved in 2012!

Leave a Comment