背景

  • 我是自动执行的PowerPoint2007年通过C#
  • 我写unittests使用的内部测试的Visual Studio(Microsoft。Visual studio.TestTools.UnitTesting)为我的代码
  • 我比较有经验的自动化办公室2007年的应用程序

我的问题

  • 当我运行了我的单元的测试,第一单元的测试方法运行良好,所有的后那有一个错误有关的一个独立自己
  • 我创造一个静态的实例的PowerPoint的测试方法来分享,但它似乎是该应用程序项越来越脱离后的第一个测试方法是运行

源代码

    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实例是使用(何时我设置可见酒店)

我已经尝试

  • 令unittests不会改变的行为
  • 同样的问题发生字与2007年,Visio2007年,等等。
  • 当编写测试用例有关,我没有得到这些问题显然的东西是不同的有关如何visual studio运行单位测试(不意味着VS是不正确的,只是指出这是不同呢)
  • 它没有任何关系,与可见性-的任何使用方法或财产,将导致这个问题
  • 我已经尝试使用的属性AssemblyInitialize和ClassInitialize创建的实例,但是没有奏效
  • Google搜索&狂欢-没有明确的答案,可以帮助我

评论意见

  • 我可以切换到关,但希望继续使用视觉工作室的地单元的测试框架

我的问题

  • 我怎么能成功地创建一个单一实例的PowerPoint2007年将在所有共享的就是裤
  • 如果你能提供深入了解 为什么 发生这种情况,我将不胜感激。

解决(由于ALCONJA)

  • 我跟随了他的建议要修改。testrunconfig和它的工作。

链接

有帮助吗?

解决方案

看起来像一个问题是,MS单元测试运行在多线程,而关试运行在相同的线。所以静态的基准以PowerPoint时跑在你的MS测试是 正在线程之间共享, ,这COM不喜欢由于通过默认其STA(单螺纹).你可以开关MS测试使用MTA(多线程COM)通过添加:

<ExecutionThread apartmentState="MTA" />

你*.testrunconfig的文件(开放式的文件作为XML&chuck上线 任何地方,在主要的 TestRunConfiguration 节点).

不知道该如何好的PowerPoint(和你的特定测试)将处理与被视为是多线程,但是你的琐碎上面的例子通过与MTA交换。如果你做得到线程问题发生的,你可以尝试使你的 单元测试下令 &看看是否能解决问题。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top