Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 100969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:40:52+00:00 2026-05-11T00:40:52+00:00

I have been trying to get more in to TDD. Currently keeping it simple

  • 0

I have been trying to get more in to TDD. Currently keeping it simple with lots of Debug.Asserts in a console application.

Part of the tests I wanted to complete was ensuring that events were raised from the object, the correct number of times, since client code will depend on these events.

So, I began thinking about how to test the events were being raised, and how I could track them. So I came up with a Monitor ‘pattern’ (if you could call it that). This is basically a class that takes an object of the type under test in its constructor.

The events are then hooked up to the monitor, with delegates created which both count and log when the event is raised.

I then go back to my test code and do something like:

    bool Test()     {         MyObject mo = new MyObject();         MyMonitor mon = new MyMonitor(mo);          // Do some tests that should cause the object to raise events..          return mon.EventCount == expectedCount;     } 

This works fine, and when I intentionally busted my code, the tests failed as expected, but I wonder, is this too much ‘free form’ test code (i.e. code without supporting tests)?


Additional Thoughts

  • Do you test for events?
  • How do you test for events?
  • Do you think the above has any holes/room for improvement?

All input gratefully received! ^_^

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:40:52+00:00Added an answer on May 11, 2026 at 12:40 am

    The easiest thing you can do is subscribe an anonymous method or a lambda to the event and increment a counter that’s local to your test in it. No need to use an extra class at all.

    I found this won’t make your code very readable so I’ve done about the same thing. I’ve written monitor objects in several projects. Usually they’re a bit more generic than your monitor. They just expose public methods that you can subscribe to events and they count the number of times they’ve been called. This way you can reuse monitor objects for different events.

    Something like this:

    MyObject subjectUnderTest = new MyObject(); EventMonitor monitor = new Monitor(); subjectUnderTest.Event += monitor.EventCatcher;  // testcode;  Assert.Equal( 1, monitor.EventsFired ); 

    The problem with this is that it’s not really generic. You can only test events that monitor.EventCatcher() can be subscribed to. I usually don’t do events with arguments so that’s no problem, I just have the standard void EventCatcher(object sender, EventArgs arguments). You could make this more generic by subscribing a lambda of the right type to the event and calling EventCatcher in the lambda. This makes your tests a bit harder to read though. You can also use generics to make the EventCatcher method work with the generic EventHandler.

    You might want to look out, eventually you’ll want to be able to store exactly what events were called in what order and with what parameters. Your eventmonitor can easilly get out of hand.


    I found another way of doing this that might make sense for tests with more complex assertions.

    Instead of creating your own monitor you let your mocking framework of choice create it for you, you just create an interface for the class that handles the event. Something like this:

    public interface IEventHandlerStub {     event EventHandler<T> Event(object sender, T arguments); } 

    Then you can mock up this interface in your test. Rhino Mocks does this like this:

    var eventHandlerStub = MockRepository.GenerateStub<IEventHandlerStub>(); myObject.Event += eventHandlerStub.Event;  // Run your code  eventHandlerStub.AssertWasCalled(x => x.Event(null, null)); 

    For a simple test like this it might be overkill but if you want to assert things about parameters for example you can use the flexibility of your mocking framework for it.

    On another note. Rob and I are working on a generic event-testing monitor class that might make some of this more easy. If people are interested in using something like this I’d like to hear from you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I'm not a RSpec-er either but I guess 'response.should be_success'… May 11, 2026 at 1:58 pm
  • added an answer I think you are out of luck with standard priority… May 11, 2026 at 1:58 pm
  • added an answer I found the solution by myself, but no documentation exists… May 11, 2026 at 1:58 pm

Related Questions

I have been trying to get around this error for a day now and
I have been trying for a long time to get SQL Server Express on
I have been having some problems trying to get my PHP running. When I
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I have been trying to figure out how to retrieve (quickly) the number of
I've been trying to create a parser using simpleparse. I've defined the grammar like
I have a list of objects and I would like to access the objects
My background is primarily as a Java Developer, but lately I have been doing

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.