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实例的行上(当我设置Visible属性时)
答案 0 :(得分:7)
看起来问题是MS单元测试在多个线程中运行,而NUnit测试在同一个线程中运行。因此,在MS测试中运行时对PowerPoint的静态引用是being shared between threads,COM不喜欢,因为默认情况下它的STA(单线程)。您可以通过添加:
来切换MS测试以使用MTA(COM的多线程)<ExecutionThread apartmentState="MTA" />
到您的* .testrunconfig文件(以XML格式打开文件,然后查看上面一行anywhere in main the TestRunConfiguration
node)。
不确定PowerPoint(以及您的特定测试)将如何处理被视为多线程,但上面的简单示例通过MTA打开。如果您确实遇到线程问题,可以尝试制作unit tests ordered&amp;看看是否能解决问题。