【问题标题】:VS2008 UnitTesting - detached RCW with Office Application objects (PowerPoint, etc.)VS2008 UnitTesting - 使用 Office 应用程序对象(PowerPoint 等)分离 RCW
【发布时间】:2009-07-13 05:22:46
【问题描述】:

背景

  • 我正在通过 C# 自动化 PowerPoint 2007
  • 我正在使用 Visual Studio 的内置单元测试 (Microsoft.VisualStudio.TestTools.UnitTesting) 为我的代码编写单元测试
  • 我在自动化 Office 2007 应用程序方面经验丰富

我的问题

  • 当我运行单元测试时,第一个单元测试方法运行良好,之后出现与分离的 RCW 相关的错误
  • 我正在创建 PowerPoint 的静态实例以供测试方法共享,但似乎在运行第一个测试方法后应用程序 RCW 正在分离

源代码

    using System;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    namespace TestDemo
    {



        [TestClass]
        public class UnitTest1
        {
            private static Microsoft.Office.Interop.PowerPoint.ApplicationClass 
              g_app = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();

            private TestContext testContextInstance;

            public TestContext TestContext
            {
                get
                {
                    return testContextInstance;
                }
                set
                {
                    testContextInstance = value;
                }
            }



            [TestMethod]
            public void Test01()
            {
                g_app.Visible = Microsoft.Office.Core.MsoTriState.msoCTrue;
            }

            [TestMethod]
            public void Test02()
            {
                g_app.Visible = Microsoft.Office.Core.MsoTriState.msoCTrue;
            }
        }

    }

错误信息

Test method TestDemo.UnitTest1.Test02 threw exception:
System.Runtime.InteropServices.InvalidComObjectException: COM 
object that has been separated from its underlying RCW cannot be used..

此消息出现在使用 PowerPoint 实例的行上(当我设置 Visible 属性时)

我已经尝试过什么

  • 单元测试的顺序不会改变行为
  • Word 2007、Visio 2007 等也会出现同样的问题。
  • 使用 NUNIT 编写测试用例时,我没有遇到这些问题 - 显然 Visual Studio 运行单元测试的方式有所不同(不是暗示 VS 不正确,只是指出它与 NUNIT 不同)
  • 它与 Visible 属性无关 - 任何方法或属性的使用都会导致此问题
  • 我尝试使用属性 AssemblyInitialize 和 ClassInitialize 来创建实例,但没有任何效果
  • Google 和 Binged - 没有明确的答案可以帮助我

评论

  • 我可以切换到 NUNIT,但更愿意继续使用 Visual Studio 的本机单元测试框架

我的问题

  • 如何成功创建将在所有 TestMethods 之间共享的 PowerPoint 2007 的单个实例
  • 如果您能提供有关为什么会发生这种情况的见解,我将不胜感激。

已解决(感谢 ALCONJA)

  • 我按照他的建议修改了 .testrunco​​nfig 并且成功了。

链接

【问题讨论】:

  • 对不起,我不能直接帮助你,但这是一个非常清晰/写得很好的问题,所以我给你一个 +1,希望能帮助你提高知名度。
  • 好吧,我撒谎了,看来我可以帮忙。请参阅下面的答案。
  • 结构很好。这对我帮助很大。

标签: visual-studio unit-testing ms-office mstest rcw


【解决方案1】:

看起来问题是 MS 单元测试在多个线程中运行,而 NUnit 测试在同一个线程中运行。因此,在 MS 测试中运行时对 PowerPoint 的静态引用是 being shared between threads,COM 不喜欢它,因为默认情况下它的 STA(单线程)。您可以通过添加以下内容来切换 MS 测试以使用 MTA(COM 多线程):

<ExecutionThread apartmentState="MTA" />

到您的 *.testrunco​​nfig 文件(以 XML 格式打开文件并夹住上面的行 anywhere in main the TestRunConfiguration node)。

不确定 PowerPoint(以及您的特定测试)如何处理被视为多线程的问题,但您上面的简单示例在 MTA 开启的情况下通过了。如果您确实遇到线程问题,您可以尝试创建您的unit tests ordered 并查看是否可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2011-07-29
    • 2011-11-21
    • 2012-04-27
    • 2023-01-25
    • 2021-09-23
    • 2011-07-03
    相关资源
    最近更新 更多