该事件是在C#中提出的单元测试 [英] Unit testing that an event is raised in C#

查看:142
本文介绍了该事件是在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.

这是提高该事件的代码是像

The code that is raising the events is like

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