Don’t call us we’ll call you

Testing how events are hooked up to your system can be tricky, TypeMock.NET makes this easier.

Testing the plumbing

Lets see how we test a class that fires events.

Here is a snippet from a List that fires event whenever an Item is added

public class ListWithChangedEvent : ArrayList { // An event that clients can use to be notified whenever the // elements of the list change. public event ChangedEventHandler ChangedEvent; // Override some of the methods that can change the list; // invoke event after each public override int Add(object value) { int i = base.Add(value); if (ChangedEvent != null) { ChangedEvent(this, EventArgs.Empty); } return i; } }

(10 point to the first to find the Race Condition bug)

To test this class we can test the following scenarios:

  1. No event listeners registered
  2. One event listener register – gets called
  3. Two event listeners registered – gets called

Testing that an event listener gets invoked is a classic interaction based test and mocking frameworks work very well with this.

Here is the second test:

[Test] public void TestEventWorks() { // Create a new list. ListWithChangedEvent list = new ListWithChangedEvent(); // Try with one listener // add a listener list.Changed += new ChangedEventHandler(list_Changed); using (RecordExpectations record = RecorderManager.StartRecording()) { // expect to be called when adding to list list_Changed(list, EventArgs.Empty); } // Add item to the list. list.Add("item 1"); // make sure it was called MockManager.Verify(); } // dummy method that is used as a placeholder void list_Changed(object sender, EventArgs e) { throw new Exception("The method or operation is not implemented."); }

There is no need to create an object that uses the ListWithChangedEvent to test the list, and with this test we can verify that the event mechanism is working.

Meanwhile at the other side…

At the other end, we have a class that registers to the event and performs some action when the event is called.

Suppose we have a class that registers to the Button.Click event


public void Example() { Button button = new Button(); button.Click += new EventHandler(button_Click); } private void button_Click(object sender, EventArgs e) { // Do Somethings here }

To test that the Example class works correctly when the button is clicked, the Button can be Mocked. It is then possible to make Example believe that the button was clicked (Note this feature requires version 3.6)

[Test] public void TestThatExampleWorksWhenButtonClicked() { using (RecordExpectations record = RecorderManager.StartRecording()) { // mock next button and the event registration Button mockedButton = new Button(); mockedButton.Click += null; } // invoke our test method that registers to button.Click Example(); // Fire the event RecorderManager.LastMockedEvent.Fire(this, EventArgs.Empty); // here we test that Example works correctly //... MockManager.Verify(); }

When the LastMockedEvent is Fired the button_Click is invoked, and we can verify that our class handles the event correctly. The heart of this exercise is the ability to mock an event registration and then fire that mocked event

  1. shopping list for your bead paramour in families doing this year. point of things you may possibly buy for this creativebead paramour is storage cases and storage units you discover his/her sources beads. There is a number of cases that can be bought in many forms of locations.
    nike id shoes quality

  2. Second, shoot pix from the engagement ring. when they are stolen, there are any great very high connected with what they appear like. in addition to, look at solutions have a tendency to. interesting engagement ring has also been intended to satisfy the becoming travel preference. people today who loved the attractive american aqua blue began to acknowledge the reccommended differences in matrix signs in addition hue, therefore. Between several my starting points.
    nike blazer pack

  3. Claire Clothes Good there is certainly conventional key elements and it’s really complex to be able to summarize most of the occasions although without many other aspects for instance diabetes, or maybe a historical past connected with coronary disease, then the standard recommendation is if repeated blood pressure measurements are more than 140 over 90 you need treatment and I would say if that’s only based on a few clinic blood pressure measurements I would require either home or 24 ambulatory blood pressure monitoring and I would want those elevated blood pressures to be confirmed on the average of the daytime readings by one of those two methods.

  4. 01 Oct 16, 2012 8:08 pm I love these! Wish I would have thought of seecph bubbles. We had a dry erase board there but someone wrote on it with the wrong pen and ruined it right at the beginning of the night!!! The photobooth pics were my fave everyone raved about it and one friend even asked for a referral cuz she wants it at her wedding I love the big red lips you got for your props and the pic of the mustaches as eyebrows is phenomenal!

Add Comment

Required fields are marked *. Your email address will not be published.