Unit tests with a WPF window

I recently encountered a problem when I ran unit tests for my WPF windows. I got this error:

One of the background threads threw exception: System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used. at System.Windows.Input.TextServicesContext.StopTransitoryExtension() at System.Windows.Input.TextServicesContext.Uninitialize(Boolean appDomainShutdown) at System.Windows.Input.TextServicesContext.TextServicesContextShutDownListener.OnShutDown(Object target, Object sender, EventArgs e) at MS.Internal.ShutDownListener.HandleShutDown(Object sender, EventArgs e)

To remove this error, simply add this method to the class where your tests are located:

[TestCleanup]
public void CleanUp()
{
    Dispatcher.CurrentDispatcher.InvokeShutdown();
}

With that there is no more problem. Here is the link to find the solution: http://connect.microsoft.com/VisualStudio/feedback/details/318333/unit-tests-which-show-windows-cause-vstesthost-mstest-shutdown-crashes-in-vs-2008-rtm

Leave a reply