简体   繁体   English

断言是否在单元测试中触发了事件获取

[英]Assert if event gets are fired within a Unit Test

I need to check if an object (EventTrigger) fires events and if I receive the expected data via the EventArgs via a unit test. 我需要检查某个对象(EventTrigger)是否触发事件,以及是否通过单元测试通过EventArgs接收到期望的数据。 In order to do that id like to loop the entire sequence for hundreds of times within my test method to assert against the event to check if the the event gets fired every time and if i get the correct data. 为了做到这一点,id希望在我的测试方法中将整个序列循环数百次,以针对该事件进行断言,以检查该事件是否每次都被触发以及我是否获得了正确的数据。

Yes, I'm fully aware that this is not a real Unit Test, its an integration test. 是的,我完全意识到这不是真正的单元测试,而是集成测试。 But the actual events get triggered by some involved hardware and that's what I aim to test. 但是实际事件是由一些涉及的硬件触发的,这就是我要测试的目标。

The problem that I have is that the test fails because the event does not get fired. 我的问题是测试失败,因为未触发该事件。 But the strange thing is that it fails randomly. 但是奇怪的是,它随机失败。 Sometimes after 2,50, 80,200 or whatever number of iterations. 有时在2,50、80,200之后或任何数量的迭代之后。

So I suspect that somehow the reference to my event handler gets lost. 因此,我怀疑对事件处理程序的引用会丢失。 I read a lot about async/await etc. but I couldn't figure out how to use it in this case. 我阅读了很多有关异步/等待等的信息,但是在这种情况下我不知道如何使用它。

    [TestMethod]
    public void FireEvents_CheckIfCorrectEventsFired_ReturnTrue()
    {
        AutoResetEvent eventFired = new AutoResetEvent(false);
        int i = 1, sum =0;

        EventTrigger.VariableChanged += (s, e) =>
        {
            if (e.VarInfo.Name.Contains("foo1"))
            {
                sum = 1;
                eventFired.Set();
            }
            else if (e.VarInfo.Name.Contains("bar2"))
            {
                sum = 2;
                eventFired.Set();
            }
        };

        while (true)
        {
            TriggerEvents();
            Assert.IsTrue(eventFired.WaitOne(3000));
            Assert.IsTrue((sum ==1) || (sum ==2));
            eventFired.Reset();
            i++;

            if (i == 100)
                break;
        }
    }

    private void TriggerEvents()
    {
      // Long running process triggering events based on external hardware
    }

EDIT: The event gets triggered on another object in this way: 编辑:以这种方式触发另一个对象上的事件:

Volatile.Read(ref VariableChanged)?.Invoke(this, new VariableChangedEventArgs(varInfo));

So basically if the HW module triggers a variable change this event gets fired and holds the information. 因此,基本上,如果硬件模块触发变量更改,则会触发此事件并保存信息。

I have tested all in detail and the event gets definitely fired and the data provided by the EventArgs are correct. 我已经详细测试了所有事件,并且肯定触发了该事件,并且EventArgs提供的数据是正确的。 Its just that in my test class (consumer) sometimes doesn't handle the event. 只是在我的测试类(消费者)中有时无法处理该事件。 I thought it could be that the reference to the event handler gets lost or whatever. 我认为可能是事件处理程序的引用丢失了或其他原因。

I simplified the code here because the actual code involves a lot of stuff that is not relevant for the actual problem. 我在这里简化了代码,因为实际代码涉及很多与实际问题无关的东西。 I banged my head on this for a while and couldn't find a way to solve my problem. 我用力撞了好一会儿,找不到解决我问题的方法。

我不确定这是否重要,但通常不必为AutoResetEvent调用Reset()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM