Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

  • http://blogs.msdn.com/martijnh/archive/2009/12/31/unit-testing-com-object-that-has-been-separated-from-its-underlying-rcw-cannot-be-used.aspx
like image 797
namenlos Avatar asked Jul 13 '09 05:07

namenlos


1 Answers

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.

like image 182
Alconja Avatar answered Sep 28 '22 20:09

Alconja