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
- Property Changed (... "My Property" ...)
- Property Changed (... "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 receivedEvents = new List ();
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]);
}
manpreet
Best Answer
2 years ago
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">events is like
I get a nice green test from the following code in my unit tests, that uses delegates:
However, if I then try and chain the setting of properties together like so:
My test for the event fails - the event that it captures is the event for the MyOtherProperty.
I'm pretty sure the event fires, my UI reacts like it does, but my delegate only captures the last event to fire.
So I'm wondering:
1. Is my method of testing events correct?
2. Is my method of raising chained events correct?