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..
This message comes on the line where the PowerPoint instance is used (when I set the Visible property)
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.