nam*_*los 7 unit-testing mstest ms-office rcw visual-studio
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Test method TestDemo.UnitTest1.Test02 threw exception:
System.Runtime.InteropServices.InvalidComObjectException: COM
object that has been separated from its underlying RCW cannot be used..
Run Code Online (Sandbox Code Playgroud)
此消息出现在使用PowerPoint实例的行上(当我设置Visible属性时)
看起来问题是MS单元测试在多个线程中运行,而NUnit测试在同一个线程中运行.因此,在MS测试中运行时对PowerPoint的静态引用是在线程之间共享的,COM不喜欢它,因为默认情况下它是STA(单线程).您可以通过添加以下命令来切换MS测试以使用MTA(COM的多线程):
<ExecutionThread apartmentState="MTA" />
Run Code Online (Sandbox Code Playgroud)
到您的*.testrunconfig文件(以XML格式打开文件并将上面的行放在主TestRunConfiguration节点的任何地方).
不确定PowerPoint(和您的特定测试)将如何处理被视为多线程,但上面的简单示例通过MTA打开.如果确实发生了线程问题,您可以尝试订购单元测试,看看是否能解决问题.
| 归档时间: |
|
| 查看次数: |
1505 次 |
| 最近记录: |