一个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(); }