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

查看:17
本文介绍了在 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 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. 我引发链式事件的方法是否正确?

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天全站免登陆