locked
Testing COM events in C# RRS feed

  • Question

  • Hello,

    I use VS2008 UnitTest to write C# tests for a COM object. The COM object has a connection-point that fire events, and I'd like to test this functionality. I try to accomplish this as follows:

    MyObject obj_ = null;
    bool fired_ = false;

    [TestMethod]
    public void testEvent
    {
      obj_ = new MyObject();
      obj_.onEvent += eventHandler;
      // ensure that this method causes the object to fire onEvent
      obj_.doSomething();

      // wait...
      // ...
      Assert.IsTrue(fired_);
    }

    private void eventHandler()
    {
      fired_ = true; 
    }

    However, I cannot "wait" for the event handler in a blocking way (Thread.Sleep() or something), because the UnitTest framework initializes the thread that calls test methods as an STA thread. So it has to pump its message-loop.

    Thus, my question is how to force the thread to be an MTA thread, OR how to do the above test in a better way.

    If I call CoUninitialize/CoInitialize in the beginning of the function, wouldn't it harm something in the framework?

    Thanks!

    Monday, June 21, 2010 1:31 PM

All replies

  • I'm experiencing the same problem here. Did you solve it?

    Thanks

    Tuesday, December 13, 2011 11:37 AM