c# - 单元测试 COM 事件?

标签 c# unit-testing com event-handling dispatcher

我们有一个用 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 上阅读了类似的问题。我已经尝试了所有建议的方法,但无法正常工作!

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

问:如何进行可以成功触发事件事件处理程序的常规 C# 单元测试?

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

最佳答案

如果您的 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();
    }
}

这有帮助吗?

关于c# - 单元测试 COM 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8501597/

相关文章:

c# - 分离数百个事件处理程序

c# - 为什么消息框从不显示?

unit-testing - Grails测试应用错误

winapi - MTA COM 服务器的实现

c# - .NET MVC 路由 - 绑定(bind) 2 个 routeMap 参数

c# - 如何从 Windows 10 获取当前 Azure Active Directory (AAD) 用户电子邮件

angular - 如何在 Bamboo 中为 Angular 项目生成代码覆盖率数据?

Java Mockito 注入(inject)静态类

c++ - get_accChildCount 在不应该返回 0 的情况下返回 0

c++ - 未按描述收到事件通知