起订量和互操作类型:在 VS2012 中有效,在 VS2010 中失败? [英] Moq & Interop Types: works in VS2012, fails in VS2010?
问题描述
我有一个包含大约 500 个单元测试的 .NET 库项目.所有这些测试在 Visual Studio 2012 中都运行良好.但是,我的一些测试在 Visual Studio 2010 中失败了.在这些失败的测试中,我使用 Moq 来模拟 Microsoft.Office 中的几种互操作类型.Interop.Excel
.尝试访问这些模拟互操作类型时,测试立即失败:
错误:缺少方法实例类 Microsoft.Office.Interop.Excel.Range [ExcelAddIn.Core] Microsoft.Office.Interop.Excel.ListRow::get_Range()"来自类Castle.Proxies.ListRowProxy"'.
这个异常意味着我忘记在我的模拟上设置适当的属性 getter.事实并非如此:
_listRowMock.Setup(m => m.Range).Returns(_rangeMock.Object);
现在我可以想象 Moq 可能不太适合互操作类型.但我觉得最令人费解的是,这些测试在 Visual Studio 2012 中运行良好,但在 Visual Studio 2010 中却失败了.
为什么我的 Visual Studio 会影响我的代码的行为?
更新:2012 年 3 月 11 日
好的,所以我把它归结为:
- 我有两个项目;Core 和 Core.UnitTest.Core 是实际的库,而 Core.UnitTest 是 Core 库的单元测试项目.
- 两个项目都引用了 Microsoft.Office.Interop.Excel 并启用了嵌入互操作类型.
- 由于启用了 EIT,两个项目都包含自己的 Microsoft.Office.Interop.Excel 库视图".该视图包括在各自项目中使用的所有类、方法和属性.
- 由于两个项目使用 Microsoft.Office.Interop.Excel 的不同类、方法和属性,两个库的嵌入类型不同.例如.Core 中的 ListRow 具有 Index 和 Range 属性,而 Core.UnitTest 中的 ListRow 仅具有 Range 属性.
- 尽管这两种类型不同并且不共享通用接口或超类,但它们为了大家的方便.它包含所有起订量参考.我有 Excel 2007(即 v12) - 所以请调整对 Office 14 的引用.
带有待测试方法的项目必须通过 .Net 参考选项卡使用 PIA
Microsoft.Office.Interop.Excel
.在单元测试项目中,您必须通过 COM 引用选项卡使用
<块引用>Microsoft Excel 1X.0 对象库
- 它是一个 ActiveX.令人困惑的是在解决方案资源管理器中,它们都被称为:Microsoft.Office.Interop.Excel
还有一个警告,我不知道如何解决 - 您必须使用 .Net 3.5 框架我实际上希望微软在 2012 年修复它,因为我无法工作用 .Net 4.0 中的所有项目来做这件事.一些针对 .Net 3.5 & 的混合项目解决方案4.0 没问题.
我在这方面遇到了很多麻烦,请参阅此处 如何在模拟 Excel.worksheet 时避免使用动态? 并查看我问的这个问题:模拟对象没有在 Intellisense 中显示所有属性 - 在一个项目中但在另一个项目中具有它们一>.
无论如何,这就是如何让它在 VS 2010 中工作.我很高兴它在 2012 年得到解决!
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 failing tests, I use Moq to mock several Interop Types from
Microsoft.Office.Interop.Excel
. The test fails immediately when attempting to access these mocked interop types:Error: Missing method 'instance class Microsoft.Office.Interop.Excel.Range [ExcelAddIn.Core] Microsoft.Office.Interop.Excel.ListRow::get_Range()' from class 'Castle.Proxies.ListRowProxy'.
This exception implies that I forgot to setup the appropriate property getter on my mock. Which is not the case:
_listRowMock.Setup(m => m.Range).Returns(_rangeMock.Object);
Now I can imagine that Moq might not work too well with Interop Types. But what I find most puzzling is that these tests run fine in Visual Studio 2012, but fail in Visual Studio 2010.
Why is my Visual Studio influencing the behavior of my code?
UPDATE: 3-11-2012
Ok, so I got it down to this:
- I have two projects; Core and Core.UnitTest. Core is the actual library while Core.UnitTest is a unit test project of the Core library.
- Both projects reference Microsoft.Office.Interop.Excel with Embed Interop Types enabled.
- Because EIT is enabled, both projects include their own "view" of the Microsoft.Office.Interop.Excel library. The view includes all classes, methods and properties that are used in their respective project.
- Because both projects use different classes, methods and properties of the Microsoft.Office.Interop.Excel, the embedded types of both libraries differ. E.g. ListRow in Core has an Index and Range property, whereas ListRow in Core.UnitTest only has the Range property.
- Although both types are different and do not share a common interface or super class, they are equivalent. This means that the CLR will treat them as if they are the same and will allow you to use these types across assembly boundaries. E.g. an instance of ListRow from Core.UnitTest will work fine when passed to a method in the Core library. The shared Range property will function, whereas the missing Index property will throw a MissingMethodException on access.
- The aforementioned behavior even works with mocked types. An mocked object of Mock[Excel.ListRow] will work fine when crossing the assembly boundary.
- Unfortunately, the behavior described in the previous point only works when I build my assemblies in Visual Studio 2012. When I build my assemblies in Visual Studio 2010 and debug my code, I can see the mocked ListRow instance being passed into a method of my Core project. The moment the instance crosses the assembly boundary, all methods and properties of ListRow lose their implementation and throw MissingMethodExceptions.
- Now for the fun part, I actually managed to mitigate this issue by ensuring that both embedded types of ListRow are aligned. E.g. in order for the compiler to create the same view of ListRow in both projects, I made sure that I used the exact same methods and properties in my UnitTest project. This means adding dummy lines like: var dummy = listRow.Index. Once I had the compiler creating identical views of my embedded ListRow type, the instance was allowed to cross assembly boundaries without losing its implementation.
The question still remains though: What causes this difference in behavior between Visual Studio 2010 and Visual Studio 2012?
UPDATE: 9-11-2012
Demo Solution: http://temp-share.com/show/KdPf6066h
I have created a small solution to demonstrate the effect. The solution consists of a library and a UnitTest project. Both reference Microsoft.Office.Interop.Excel.Range with EIT enabled. The test works fine in VS2012 but throws MissingMethodException in VS2010. Uncommenting the dummy line in the test will make it work in VS2010.
FINAL UPDATE: 29-12-2012
My apologies for the late update. A colleague of mine found a solution, however I was unable to reproduce it on my machine. In the meantime our company has made the switch to TFS2012 so this is no longer a blocking issue for me. The two most important conclusions my colleague made were:
- The semantics of the "Any CPU" platform have changed from Visual Studio 2010 to Visual Studio 2012. This will result in different .DLL's being generated depending on whether you are using VS2010 or VS2012.
- Both projects referenced different versions of Microsoft.Office.Interop.Excel.
I checked my projects and straightened out the references, but it made no difference. After that, I tried different variations of platforms in both VS2010 and VS2012 but was unable to produce a satisfactory result. I will accept Jeremy's answer as it was the most helpful. Thank you all for your assistance.
解决方案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:
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!
这篇关于起订量和互操作类型:在 VS2012 中有效,在 VS2010 中失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!