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 8607151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:16:41+00:00 2026-06-12T03:16:41+00:00

I have defined two events with custom event arguments and associated raising methods. Now

  • 0

I have defined two events with custom event arguments and associated raising methods. Now I wonder what and how the event should be tested. How should I analyze the code to find candidates for unit testing?

  • 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-06-12T03:16:43+00:00Added an answer on June 12, 2026 at 3:16 am

    The way I test events is as follows:

    Suppose this is your object:

    public class MyEventRaiser
    {
        public event EventHandler<string> MyEvent = delegate { };
    
        public void Process(string data)
        {
            // do something interestuing
    
            Thread.Sleep(2000);
    
            if (!string.IsNullOrEmpty(data))
            {
                this.MyEvent(this, data + " at: " + DateTime.Now.ToString());
            }
        }
    }
    

    Therefore your Subject Under Test is: MyEventRaiser and you want to test the method Process. You need to test that the event is raised when certain conditions are met, otherwise, the event should not be raised.

    To do it, I use this framework (that I use always in my tests) FluentAssertions, this framewrok can be used with any test engine like MSTest, NUnit, MSpec, XUnit, etc

    The tests look like:

    [TestClass]
    public class CustomEventsTests
    {
        [TestMethod]
        public void my_event_should_be_raised()
        {
            var sut = new MyEventRaiser();
    
            sut.MonitorEvents();
    
            sut.Process("Hello");
    
            sut.ShouldRaise("MyEvent").WithSender(sut);
        }
    
        [TestMethod]
        public void my_event_should_not_be_raised()
        {
            var sut = new MyEventRaiser();
    
            sut.MonitorEvents();
    
            sut.Process(null);
    
            sut.ShouldNotRaise("MyEvent");
        }
    }
    

    You need to use the following namespaces:

    using FluentAssertions;
    using FluentAssertions.EventMonitoring;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface defined as IStore, with two methods: public interface IStore<TEntity> {
I have a function called DrawPlaybook which listens to two events, one mouseclick event
I have defined two endpoints in my App.Config file as <system.serviceModel> <services> <service name=HostDirectAddress.ITestService
I have defined the following two functions test <- function(t) { return( (0.5*eta^2/theta)*(1-exp(-2*theta*t)) )
if I have two classes, and have defined an explicit type conversion between them,
I'm trying to draw two sprites on the same screen. I have defined the
Friends, I have two files, a.c and b.c . I have defined a function
I have two factories defined (there are others not shown) as follows: Jobseekers &
Preface: I have two entities defined as a one-to-many relationship: A <<-------> B. B's
Assuming you have two lists defined in an xml schema, call them A and

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.