If your COM object is an STA, you probably need to start a message loop so that its events light up.
To do this, you can use a small wrapper around the Application
and Form
objects. Here is a small example that I wrote in a few minutes.
Please note that I have not run and tested it, so it may not work, and cleaning should be better. But this may give you direction to decide.
Using this approach, the test class will look something like this:
[TestMethod] public void Test() { MessageLoopTestRunner.Run(
And the code for MessageLoopTestRunner
would be something like this:
public interface IMessageLoopTestRunner { void Finish(); } public class MessageLoopTestRunner : Form, IMessageLoopTestRunner { public static void Run(Action<IMessageLoopTestRunner> test, TimeSpan timeout) { Application.Run(new MessageLoopTestRunner(test, timeout)); } private readonly Action<IMessageLoopTestRunner> test; private readonly Timer timeoutTimer; private MessageLoopTestRunner(Action<IMessageLoopTestRunner> test, TimeSpan timeout) { this.test = test; this.timeoutTimer = new Timer { Interval = (int)timeout.TotalMilliseconds, Enabled = true }; this.timeoutTimer.Tick += delegate { this.Timeout(); }; } protected override void OnLoad(EventArgs e) { base.OnLoad(e);
Does it help?
Ran
source share