Getting a "COM object that has been separated from its underlying RCW cannot be used" error when running tests in Visual Studio?
If it's because you're opening WPF windows, then the solution could be quite simple...
Say you have this as part of one of your tests:
<TestMethod()>
Public Sub TestWindow()
Dim window As New Window
. . .
window.Show()
. . .
End Sub
For instance, I often test parts of user controls (localized labels, simple behavior on mouse clicks / keyboard events, view-view model interactions, etc.)
using set ups similar to this one:
<TestMethod()>
Public Sub TestWindow()
Dim view As New View ' my user control
Dim viewModel As New ViewModel ' the view model for the user control
Dim window As New Window
view.DataContext = viewModel
window.Content = view
window.Show()
. . . ' and the main part of the test goes here
End Sub
However, when you run these test using MSTest, the test itself will pass, but you'll get a test run error for the whole test run:
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)
I couldn't find a good description of what exactly causes this error;
I guess there's a conflict between the test manager shutting down the thread in which the test runs,
with the dispatcher trying to process WPF events... (???)
Anyway, a way to resolve this issue is to add the following code to the TestCleanup method:
<TestCleanup()>
Public Sub CleanupTest()
Dispatcher.CurrentDispatcher.InvokeShutdown()
End Sub
This, I guess, forces the current dispatcher to shut down *synchronously* -
so, before the end of the test, and before the test thread gets destroyed.
The important thing, though, is that it usually works (at least for me). :-)
HTH
Top
|