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

  • SEARCH
  • Home
  • 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 748875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:22:13+00:00 2026-05-14T14:22:13+00:00

I want to test that setting a certain property (or more generally, executing some

  • 0

I want to test that setting a certain property (or more generally, executing some code) raises a certain event on my object. In that respect my problem is similar to Unit testing that an event is raised in C#, but I need a lot of these tests and I hate boilerplate. So I’m looking for a more general solution, using reflection.

Ideally, I would like to do something like this:

[TestMethod]
public void TestWidth() {
    MyClass myObject = new MyClass();
    AssertRaisesEvent(() => { myObject.Width = 42; }, myObject, "WidthChanged");
}

For the implementation of the AssertRaisesEvent, I’ve come this far:

private void AssertRaisesEvent(Action action, object obj, string eventName)
{
    EventInfo eventInfo = obj.GetType().GetEvent(eventName);
    int raisedCount = 0;
    Action incrementer = () => { ++raisedCount; };
    Delegate handler = /* what goes here? */;

    eventInfo.AddEventHandler(obj, handler);
    action.Invoke();
    eventInfo.RemoveEventHandler(obj, handler);

    Assert.AreEqual(1, raisedCount);
}

As you can see, my problem lies in creating a Delegate of the appropriate type for this event. The delegate should do nothing except invoke incrementer.

Because of all the syntactic syrup in C#, my notion of how delegates and events really work is a bit hazy. This is also the first time I dabble in reflection. What’s the missing part?

  • 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. Editorial Team
    Editorial Team
    2026-05-14T14:22:14+00:00Added an answer on May 14, 2026 at 2:22 pm

    I recently wrote a series of blog posts on unit testing event sequences for objects that publish both synchronous and asynchronous events. The posts describe a unit testing approach and framework, and provides the full source code with tests.

    I describe the implementation of an “event monitor” which allows writing event sequencing unit tests to be written more cleanly i.e. getting rid of all the messy boilerplate code.

    Using the event monitor described in my article, tests can be written like so:

    var publisher = new AsyncEventPublisher();
    
    Action test = () =>
    {
        publisher.RaiseA();
        publisher.RaiseB();
        publisher.RaiseC();
    };
    
    var expectedSequence = new[] { "EventA", "EventB", "EventC" };
    
    EventMonitor.Assert(publisher, test, expectedSequence);
    

    Or for a type that implements INotifyPropertyChanged:

    var publisher = new PropertyChangedEventPublisher();
    
    Action test = () =>
    {
        publisher.X = 1;
        publisher.Y = 2;
    };
    
    var expectedSequence = new[] { "X", "Y" };
    
    EventMonitor.Assert(publisher, test, expectedSequence);
    

    And for the case in the original question:

    MyClass myObject = new MyClass();
    EventMonitor.Assert(myObject, () => { myObject.Width = 42; }, "Width");
    

    The EventMonitor does all the heavy lifting and will run the test (action) and assert that events are raised in the expected sequence (expectedSequence). It also prints out nice diagnostic messages on test failure. Reflection and IL are used under the hood to get the dynamic event subscription working, but this is all nicely encapsulated, so only code like the above is required to write event tests.

    There’s a lot of detail in the posts describing the issues and approaches, and source code too:

    http://gojisoft.com/blog/2010/04/22/event-sequence-unit-testing-part-1/

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

Sidebar

Related Questions

No related questions found

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.