Inventor API 在测试环境下的行为与在开发环境下的行为不同

问题描述

我们正在开发 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 的文档是 here。 当我在 Inventor 下运行我们的 AddIn 时,这个函数被调用,它按预期正常工作。但是,当我编写单元测试以在我的测试代码中(在同一个项目中)调用这个完全相同的函数时,它会在调用 AddButtonDefinition 的行抛出以下 COM 异常:

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

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

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

解决方法

在我看来,这是 COM(或类似的东西)的限制。您无法在进程之间访问/共享 id foo bar baz attr4 attr5 1: 1 NA 2 2 1 5 2: 1 NA 2 2 1 5 3: 1 NA 2 2 1 5 4: 2 3 NA 2 1 2 5: 2 3 3 1 3 2 6: 2 3 3 1 4 2 7: 3 5 2 2 1 3 8: 3 5 2 2 1 3 9: 4 NA NA NA NA NA 类型的对象。我尝试在外部应用程序(也是 MSTest)中读取现有 IPictureDispStandardIcon,我得到 Controldefinition。但是相同的代码在 AddIn 中也能工作。

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...