一个WPF程序UnitTest发生错误的解决办法

[TestMethod]
public void Test()
{
    Window window = new Window();
    window.Show();
    window.Close();
    window = null;
}

以上代码会引发一个异常,异常消息如下:

"An unhandled exception of type 'System.Runtime.InteropServices.InvalidComObjectException' occurred in PresentationCore.dll Additional information: COM object that has been separated from its underlying RCW cannot be used."

解决办法: 在TestCleanup()中调用CurrentDispatcher.InvokeShutdown() 。

[TestCleanup]
public void TestCleanup()
{
    System.Windows.Threading.Dispatcher.CurrentDispatcher.InvokeShutdown();
}

参考: Tricks for Writing Unit Tests for WPF Applications

posted on 2011-05-17 15:16  Ivan Sun  阅读(683)  评论(0编辑  收藏  举报

导航