VS2008 UnitTesting - detached RCW with Office Application objects (PowerPoint, etc.)
Asked Answered
C

1

7

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

Costermonger answered 13/7, 2009 at 5:22 Comment(3)
Sorry I can't directly help you, but this is an extremely clear/well written question, so I'm throwing you a +1 in hopes of helping get you some more visibility.Horseflesh
Ok, I lied, looks like I can help. See my answer below.Horseflesh
Great structure. It helped me a lot.Eighth
H
7

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.

Horseflesh answered 13/7, 2009 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.