0

我们正在开发一个Autodesk Inventor AddIn程序,并且我们正在使用MsTest框架进行一些集成测试。这是一个非常简单的函数,它创建一个带有图标的按钮定义。

public ButtonDefinition GetButtonDef(Inventor.Application app, string m_clientId)
{
    stdole.IPictureDisp icon16 = PictureDispConverter.ToIPictureDisp(InvAddIn.Properties.Resources.cp_logo_16x16);
    stdole.IPictureDisp icon32 = PictureDispConverter.ToIPictureDisp(InvAddIn.Properties.Resources.cp_logo_32x32);
    m_buttonDefinition = app.CommandManager.ControlDefinitions.AddButtonDefinition(
                                         "My Command2", "MyCommand.InternalName", 
                                         CommandTypesEnum.kShapeEditCmdType, m_clientId, 
                                         "My description text","My tooltip text", icon16, icon32, 
                                         ButtonDisplayEnum.kAlwaysDisplayText
                                         );
    return m_buttonDefinition;
}

API 的文档在这里。当我在 Inventor 下运行我们的 AddIn 时,会调用此函数并且它按预期正常工作。但是,当我编写单元测试以在我的测试代码(在同一个项目中)中调用这个完全相同的函数时,它会在调用 AddButtonDefinition 的行中引发以下 COM 异常:

 System.Runtime.InteropServices.COMException: Catastrophic error (Exception of HRESULT: 0x8000FFFF (E_UNEXPECTED))

经过长时间的尝试我才发现,当两个图标(icon16,icon32作为参数)都被省略(即替换为null)时,测试代码不会抛出异常。

我知道这个问题可能太具体了,但是有人可以给我一些一般性的提示:API 在测试环境下的行为如何不同,可能的原因是什么?您的意见将不胜感激!

4

1 回答 1

1

在我看来,这是 COM(或类似的东西)的限制。您不能IPictureDisp在进程之间访问/共享类型的对象。我尝试阅读外部应用程序StandardIcon中存在Controldefinition的内容(MSTest 也是),我得到了COMException. 但是相同的代码在 AddIn 中也可以工作。

我使用 MSTest 对业务逻辑进行单元测试,但不用于 GUI 创建。对于此集成测试,您可以使用插件启动 Inventor,然后检查用户控件是否已定义以及是否存在于相应的功能区选项卡和面板中。

于 2021-07-27T09:22:17.113 回答