在C#中引发事件的单元测试(按顺序) [英] Unit testing that events are raised in C# (in order)

查看:171
本文介绍了在C#中引发事件的单元测试(按顺序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些引发 PropertyChanged 事件的代码,我希望能够对事件是否正确引发进行单元测试。

I have some code that raises PropertyChanged events and I would like to be able to unit test that the events are being raised correctly.

引发事件的代码是

public class MyClass : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;  

   protected void NotifyPropertyChanged(String info)
   {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
   }  

   public string MyProperty
   {
       set
       {
           if (_myProperty != value)
           {
               _myProperty = value;
               NotifyPropertyChanged("MyProperty");
           }
       }
   }
}

I从我的单元测试中的以下代码中得到一个不错的绿色测试,该测试使用了委托:

I get a nice green test from the following code in my unit tests, that uses delegates:

[TestMethod]
public void Test_ThatMyEventIsRaised()
{
    string actual = null;
    MyClass myClass = new MyClass();

    myClass.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
    {
         actual = e.PropertyName;
    };

    myClass.MyProperty = "testing";
    Assert.IsNotNull(actual);
    Assert.AreEqual("MyProperty", actual);
}

但是,如果我尝试将属性设置链接在一起,如下所示:

However, if I then try and chain the setting of properties together like so:

public string MyProperty
{
    set
    {
        if (_myProperty != value)
        {
            _myProperty = value;
            NotifyPropertyChanged("MyProperty");
            MyOtherProperty = "SomeValue";
        }
    }
}

public string MyOtherProperty
{
    set
    {
        if (_myOtherProperty != value)
        {
            _myOtherProperty = value;
            NotifyPropertyChanged("MyOtherProperty");
        }
    }
}

我对该活动的测试失败-它捕获的事件是MyOtherProperty的事件。

My test for the event fails - the event that it captures is the event for the MyOtherProperty.

我很确定该事件会触发,我的UI会像它一样反应,但是我的委托人仅捕获了

I'm pretty sure the event fires, my UI reacts like it does, but my delegate only captures the last event to fire.

所以我想知道:

1.我测试事件的方法正确吗?

2.我提出 chained 事件的方法是否正确?

So I'm wondering:
1. Is my method of testing events correct?
2. Is my method of raising chained events correct?

推荐答案

您所做的一切都是正确的,只要您要测试询问上一次引发的事件是什么?

Everything you've done is correct, providing you want your test to ask "What is the last event that was raised?"

您的代码按以下顺序触发了这两个事件

Your code is firing these two events, in this order


  • 属性更改(...我的财产 ...)

  • 属性已更改(... MyOtherProperty ...)

是否正确是取决于这些事件的目的。

Whether this is "correct" or not depends upon the purpose of these events.

如果要测试事件数可以提高现有的测试范围,并且可以轻松扩展现有测试:

If you want to test the number of events that gets raised, and the order they get raised in, you can easily extend your existing test:

[TestMethod]
public void Test_ThatMyEventIsRaised()
{
    List<string> receivedEvents = new List<string>();
    MyClass myClass = new MyClass();

    myClass.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
    {
        receivedEvents.Add(e.PropertyName);
    };

    myClass.MyProperty = "testing";
    Assert.AreEqual(2, receivedEvents.Count);
    Assert.AreEqual("MyProperty", receivedEvents[0]);
    Assert.AreEqual("MyOtherProperty", receivedEvents[1]);
}

这篇关于在C#中引发事件的单元测试(按顺序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆