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

后端 未结 4 714
攒了一身酷
攒了一身酷 2020-12-03 11:43

I have a .NET library project with about 500 unit tests. All these tests run fine in Visual Studio 2012. However, some of my tests fail in Visual Studio 2010. In these faili

相关标签:
4条回答
  • 2020-12-03 12:16

    I found that, at least in VS2015, I could still embed the interop types in my assembly under test, but set "Embed" to false on the PIA assembly reference in my test project and I no repro this.

    0 讨论(0)
  • 2020-12-03 12:25

    I tried to reproduce this, and for me it doesn't work even in VS 2012.

    When you compile a project using "Embed Interop Types", the C# compiler generates an internal type that has only the members you are accessing, and the implementation actually seems to use IDispatch to call the method of the COM object by id.

    From your description I understand that your test project in VS 2012 does not access the properties (not even in order to mock them), but the test still succeeds and the generated type in the test project does have these members.

    If this is indeed what you are experiencing, can you please look at the content of your test .dll and see how the interop type was generated? You can use a tool such as ildasm.exe.

    If the interop type in the test .dll contains all the members, even those you don't access in the test, this may give a clue that the difference is related to the way the interop types are generated in VS 2012.

    Also, if you can attach a small minimal VS 2012 solution which reproduces the problem, it may greatly help in diagnosing this.

    0 讨论(0)
  • 2020-12-03 12:30

    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!

    0 讨论(0)
  • 2020-12-03 12:34

    First of all check while adding library to the project in VS2010, make sure you created the mock object like

    Mock<DocumentService> _mock = new Mock<DocumentService>();
    

    Also .NET 4.0 allows primary interop assemblies to be embedded into your assembly so that you don't need to deploy them alongside your application.Open the properties tab in the assembly in VS2010 and check the Embed Interop types.mkae sure its true.

    And to instantiate excel, Excel.Application xlapp = new Excel.Application();

    Hope it will work..

    0 讨论(0)
提交回复
热议问题