Note that there are some explanatory texts on larger screens.

plurals
  1. POVS2008 UnitTesting - detached RCW with Office Application objects (PowerPoint, etc.)
    text
    copied!<h2>BACKGROUND</h2> <ul> <li>I am automating an PowerPoint 2007 via C#</li> <li>I am writing unittests using the built-in unit testing of Visual Studio (Microsoft.VisualStudio.TestTools.UnitTesting) for my code</li> <li>I am well relatively experienced in automating the Office 2007 apps</li> </ul> <h2>MY PROBLEM</h2> <ul> <li>When I run my unit tests, the first unit test method runs fine, all after that have an error concerning a detached RCW</li> <li>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</li> </ul> <h2>THE SOURCE CODE</h2> <pre><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; } } } </code></pre> <h2>THE ERROR MESSAGE</h2> <pre><code>Test method TestDemo.UnitTest1.Test02 threw exception: System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.. </code></pre> <p>This message comes on the line where the PowerPoint instance is used (when I set the Visible property)</p> <h2>WHAT I HAVE TRIED</h2> <ul> <li>The order of the unittests does not change the behavior</li> <li>The same problem occurs with Word 2007, Visio 2007, etc.</li> <li>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)</li> <li>It has nothing to do with the Visible property - any usage of a method or property will cause this issue</li> <li>I have tried using the attributes AssemblyInitialize and ClassInitialize to create the instance but nothing has worked </li> <li>Googled &amp; Binged - no clear answer that helps me</li> </ul> <h2>COMMENTS</h2> <ul> <li>I could switch to NUNIT, but would prefer to keep using Visual Studio's native unit test framework </li> </ul> <h2>MY QUESTION</h2> <ul> <li>How can I successfully create an single instance of PowerPoint 2007 that will be shared among all the TestMethods</li> <li>If you can provide insight into <em>why</em> this is happening, I would be grateful.</li> </ul> <h2>SOLVED (THANKS TO ALCONJA)</h2> <ul> <li>I followed his advice to modify the .testrunconfig and it worked. </li> </ul> <h2>LINKS</h2> <ul> <li><a href="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" rel="nofollow noreferrer">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</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload