单元测试COM事件

本文关键字:事件 COM 单元测试 | 更新日期: 2023-09-27 18:17:43

我们有一个用c++编写的自制COM组件。现在我们想在c#测试项目中测试它的函数和事件。函数测试非常直接。但是,这些事件永远不会被触发。

MyLib.MyClass m = new MyLib.MyClass();
Assert.IsTrue(m.doStuff()); // Works
// This does not work. OnMyEvent is never called!
m.MyEvent += new MyLib.IMyClassEvents_MyEventHandler(OnMyEvent);
m.triggerEvent();

我在谷歌上搜索了这个,并在StackOverflow上阅读了类似的问题。我已经尝试了所有建议的方法,但就是行不通!

到目前为止,我已经尝试使用活动调度程序运行我的测试,但没有成功。我还尝试使用Dispatcher.PushFrame()在主线程中手动泵送消息。什么都没有。我的事件从不触发。我创建了一个简单的WinForms项目,并验证了我的事件在正常设置下工作。因此,此问题仅适用于单元测试。

问:我如何做一个常规的c#单元测试,可以成功地触发活动的事件处理程序?

应该有人有一个工作样本!请帮助。

单元测试COM事件

如果你的COM对象是一个STA对象,你可能需要运行一个消息循环来使它的事件触发。

您可以在ApplicationForm对象周围使用一个小包装来做到这一点。这是我在几分钟内写的一个小例子。

请注意,我没有运行或测试它,所以它可能无法工作,并且清理可能应该更好。但它可能会给你一个解决问题的方向。

使用这种方法,测试类看起来像这样:
[TestMethod]
public void Test()
{
    MessageLoopTestRunner.Run(
        // the logic of the test that should run on top of a message loop
        runner =>
        {
            var myObject = new ComObject();
            myObject.MyEvent += (source, args) =>
            {
                Assert.AreEqual(5, args.Value);
                // tell the runner we don't need the message loop anymore
                runner.Finish();
            };
            myObject.TriggerEvent(5);
        },
        // timeout to terminate message loop if test doesn't finish
        TimeSpan.FromSeconds(3));
}

MessageLoopTestRunner的代码应该是这样的:

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);
        // queue execution of the test on the message queue
        this.BeginInvoke(new MethodInvoker(() => this.test(this)));
    }
    private void Timeout()
    {
        this.Finish();
        throw new Exception("Test timed out.");
    }
    public void Finish()
    {
        this.timeoutTimer.Dispose();
        this.Close();
    }
}

有帮助吗?