VS2008 UnitTesting - 与 Office 应用程序对象(PowerPoint 等)分离的 RCW

发布于 2024-07-26 07:44:37 字数 2734 浏览 1 评论 0 原文

背景

  • 我正在通过 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 等。
  • 我没有遇到这些问题 - 显然 Visual Studio 运行单元测试的方式有所不同(并不是暗示 VS 不正确,只是指出它与 NUNIT 不同)
  • 当使用 NUNIT 编写测试用例时, 与 Visible 属性无关 - 任何方法或属性的使用都会导致此问题
  • 我尝试使用属性 AssemblyInitialize 和 ClassInitialize 来创建实例,但没有任何
  • 效果 Binged - 没有明确的答案可以帮助我

评论

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

我的问题

  • 如何成功创建将在所有测试方法之间共享的 PowerPoint 2007 的单个实例
  • 如果您如果您能深入了解为什么会发生这种情况,我将不胜感激。

已解决(感谢 ALCONJA)

  • 我按照他的建议修改了 .testrunco​​nfig 并且它起作用了。

链接

BACKGROUND

  • I am automating an PowerPoint 2007 via C#
  • I am writing unittests using the built-in unit testing of Visual Studio (Microsoft.VisualStudio.TestTools.UnitTesting) for my code
  • I am well relatively experienced in automating the Office 2007 apps

MY PROBLEM

  • When I run my unit tests, the first unit test method runs fine, all after that have an error concerning a detached RCW
  • I am creating a static instance of PowerPoint for the test methods to share, but it seems like the application RCW is getting detached after the first test method is run

THE SOURCE CODE

    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;
            }
        }

    }

THE ERROR MESSAGE

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

This message comes on the line where the PowerPoint instance is used (when I set the Visible property)

WHAT I HAVE TRIED

  • The order of the unittests does not change the behavior
  • The same problem occurs with Word 2007, Visio 2007, etc.
  • When writing testcases with NUNIT I don't get these problem - obviously something is different about how visual studio runs unit tests (not implying VS is incorrect, just pointing out it is different from NUNIT)
  • It has nothing to do with the Visible property - any usage of a method or property will cause this issue
  • I have tried using the attributes AssemblyInitialize and ClassInitialize to create the instance but nothing has worked
  • Googled & Binged - no clear answer that helps me

COMMENTS

  • I could switch to NUNIT, but would prefer to keep using Visual Studio's native unit test framework

MY QUESTION

  • How can I successfully create an single instance of PowerPoint 2007 that will be shared among all the TestMethods
  • If you can provide insight into why this is happening, I would be grateful.

SOLVED (THANKS TO ALCONJA)

  • I followed his advice to modify the .testrunconfig and it worked.

LINKS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新一帅帅 2024-08-02 07:44:37

看起来问题在于 MS 单元测试在多个线程中运行,而 NUnit 测试在同一线程中运行。 因此,在 MS 测试中运行时对 PowerPoint 的静态引用为 在线程之间共享,这是 COM 不喜欢的,因为默认情况下它是 STA(单线程)。 您可以将 MS 测试切换为使用 MTA(COM 多线程),方法是添加:

<ExecutionThread apartmentState="MTA" />

到您的 *.testrunco​​nfig 文件(以 XML 格式打开该文件并删除上面的行 main TestRunConfiguration 节点中的任何位置)。

不确定 PowerPoint (以及您的特定测试)将如何处理被视为多线程,但上面的简单示例在 MTA 打开时通过。 如果确实出现线程问题,您可以尝试订购单元测试 & 看看是否可以解决问题。

Looks like the issue is that MS Unit Tests run in multiple threads whereas NUnit tests run in the same thread. So the static reference to PowerPoint when running in your MS tests is being shared between threads, which COM doesn't like since by default its STA (single threaded). You can switch MS test to use MTA (multi-threading for COM) by adding:

<ExecutionThread apartmentState="MTA" />

to your *.testrunconfig file (open the file as XML & chuck the above line anywhere in main the TestRunConfiguration node).

Not sure how well PowerPoint (& your specific tests) will deal with being treated as being multi-threaded, but your trivial example above passes with MTA switched on. If you do get threading issues occurring, you could try making your unit tests ordered & see if that fixes the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文