Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech QA/Testing 2 years ago
Posted on 16 Aug 2022, this text provides information on QA/Testing related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
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.
PropertyChanged
The code that is raising the events">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"); } } }
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?
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
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]); }
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 10 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 9 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.