Hi,
i'm doing some test using the .NET 2.0 TestTools and Visual Studio 2005. The code below is pseudo code just to demonstrate what i'm doing. Im testing a singleton class that creates an COM Instance somewhere during construction/initialization, so the classes state will be keep the same in all tests. The Problem is, each test runs for itself, running all test will give a strange result - the first test succeeds, all other fail (see below, Scenario A). Wrapping the 'tests' in a single test method (see below, Scenario B) will run all former test methods (not longer marked with the [TestMethod] attrib) with success.
Scenario A always leads to the following exception: "System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.." - result of a corrupted COM reference (see myComInstance below).
Anyone experienced the same problem with corrupted COM references during unit testing Have found a thread in the J# forums where this exception is discussed, but the solution doesn't fit to my problem. My thought is, that in vs2005 each testmethod runs in it's own thread and therefore the COM ref gets corrupted.
Not quite sure if i explained the problem correctly (my english has got worse over the last couple of years).
thanx, mm
p.s. here's the pseudo code
// >> Class to test -------------------------------
public class MyClass : IDisposable{
public static MyClass Instance = new MyClass(); private ThirdPartySDK.ServerClass _myComInstance = null; private MyClass(){
// Singleton_myComInstance =
new ThirdPartySDK.ServerClass();}
public void DoSomeThing(){
_myComInstance.DoSomeThing(
"important stuff");}
public void DoSomeOtherThing(){
_myComInstance.DoSomeOtherThing(
"more important stuff");}
public void Dispose(){
// the stdandard dispose stuff, creating // an Dispose(bool) and releasing // the COM Ref via Marshal.ReleaseComObject(_myComInstance);}
}
// >> Tests (Scenario A) -------------------------------[TestMethod]
public void Test_DoSomeThing(){
MyClass.Instance.DoSomeThing();}
[TestMethod]
public void Test_DoSomeOtherThing(){
MyClass.Instance.DoSomeOtherThing();}
// >> Tests (Scenario B) -------------------------------[TestMethod]
public void WrappedTests(){
Test_DoSomeThing();
Test_DoSomeOtherThing();
}
public void Test_DoSomeThing(){
MyClass.Instance.DoSomeThing();}
public void Test_DoSomeOtherThing(){
MyClass.Instance.DoSomeOtherThing();}

System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW...
Pak Lui
jpcesar
I can't find any documentation to answer that particular question. I did find this interesting text though in regards to remoting, which we have to contend with in our application from MSDN..it's almost like they're saying all rules are off when remoting:
Not sure if I follow all of that, but I think they're basically saying Singleton isn't truely singleton when it comes to remoting as I'm (and probably others) are use to viewing Singleton.
And also in threading best practices they say to avoid having multiple threads point to the same resource, so I guess the question is how to make your unit tests run on a single thread
I hope you can find a better answer...I'll be watching for one.