Caves Travel Diving Graphics Mizar Texts Cuisine Lemkov Contact Map RSS Polski
Trybiks' Dive Texts Testing VS - Test Run Error - "COM object that has been separated from its underlying RCW cannot be used" YAC Software
  Back

List

Charsets

Charts

DBExpress

Delphi

HTML

Intraweb

MSTest

PHP

Programming

R

Rhino Mocks

Software

Testing

UI Testing

VB.NET

VCL

WPF

VS - Test Run Error - "COM object that has been separated from its underlying RCW cannot be used"
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

Comments
Alas!
No comments yet...

Top

Add a comment (fields with an asterisk are required)
Name / nick *
Mail (will remain hidden) *
Your website
Comment (no tags) *
Enter the text displayed below *
 

Top

Tags

Testing

WPF

MSTest


Related pages

AssertWasCalled and Methods Called Multiple Times

AssertWas[Not]Called and Object Properties

Rhino Mocks's AssertWasNotCalled

Visual Studio - moving coded UI tests to a new / different project results in null reference exceptions

PrivateObject and Out/ByRef parameters

PrivateObject, WithEvents, and generics

Accessing private members of base classes

PrivateObject and WithEvents

Accessing private and protected members - PrivateObject and PrivateType

Get the TreeViewItem for an Item in a WPF TreeView

Double Clicks in WPF TreeView Controls

Output in MSTest Tests (VS 2010)

Automated WPF tests and "Invalid URI: Invalid port specified."

Checking Property Change Notifications

Checking "Dangling" Event Handlers in Delphi Forms

Rhino Mocks's AssertWasCalled in VB.NET

First steps with Rhino Mocks (in VB.NET)

VS Pending Tests

Automated GUI Testing

Automated GUI Testing in VMs

Automated Testing of Window Forms

Detecting Memory Leaks with DUnit