【问题标题】:Inventor API behave differently under test environment than it under development environmentInventor API 在测试环境下的行为与在开发环境下的行为不同
【发布时间】:2021-10-02 23:38:28
【问题描述】:

我们正在开发一个 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 在测试环境下的行为为何不同,可能的原因是什么? 非常感谢您的意见!

【问题讨论】:

    标签: c# api unit-testing mstest autodesk-inventor


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回答。我想我们的问题是业务逻辑与类中的 GUI 创建有点混合。与我们团队中的所有其他人相比,您的解释最有意义。我把这篇文章留了几天,如果没有更好的建议,我会接受你的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 2016-07-09
    • 2014-02-16
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多