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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:18:03+00:00 2026-06-12T04:18:03+00:00

I have an event and a raising method as below : public class Events

  • 0

I have an event and a raising method as below :

public class Events {

    public event EventHandler<CustomEventArgs> Succeed;

    public virtual void OnSucceed(object sender, params object[] data)
    {
        CustomEventArgs args = new CustomEventArgs(data);

        EventHandler<CustomEventArgs> _succeed = Succeed;

        if (_succeed != null)
        {
            _succeed(sender, args);
        }

    }}

I have created a unit test for the OnSucceed method (using FluentAssertions ):

    [Test]
    public void SucceedShouldNotBeRaisedTest()
    {
        Events events = new Events();

        events.MonitorEvents();

        events.OnSucceed(this,"somedata");

        events.ShouldNotRaise("Succeed");
    }

as there is no subscriber to the event then I expect it not to raise Succeed event

but the test fails as Succeed event is raised . what’s wrong with this ?!

  • 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-12T04:18:05+00:00Added an answer on June 12, 2026 at 4:18 am

    When you call events.MonitorEvents();, FluentAssertions automatically subscribe to the public events to detect when an event has been raised.

    Your test is failing because your condition will always evaluate to true: if (_succeed != null). When testing, the event will always be different from null

    Now I would like to recommend you to following approach proposed by Jon Skeet:

    public event EventHandler<CustomEventArgs> Succeed = delegate { } ;
    

    With the above event declaration, your event will never be null (an event cannot be assigned from outside of its class)

    Note: You can assign the delegate behind the event to null inside its class like this:

    this.Succeed = null;
    

    The above statement is assigning the delegate behind the event to null, not the event itself. Usually you won’t need to do something like this, but in case you do, you would have to re-initialize the event like this:

    this.Succeed = null;
    this.Succeed = delegate { };
    

    If you follow these suggestions, your event will never be null and you won’t need to call the if(this.MyEvent != null) condition in order to raise your events anymore. (Note that this condition was totally technical and it’s not related to the domain itself.)

    Now that you have removed that technical condition you can actually focus in the domain rules to decide when to raise the event.

    The last step would be to remove: if (_succeed != null) and add a condition indicating if the event should or should not be raised based on your current domain

    if(shouldRaiseEvent)
    {
        EventHandler<CustomEventArgs> _succeed = Succeed;
    
        _succeed(...);
    }
    

    For your tests, you just need to configure your Subject Under Test with the required conditions in order to raise or not raise your events.

    Full sample:

    public class Events {
    
    public event EventHandler<CustomEventArgs> Succeed = delegate { };
    
    public virtual void OnSucceed(object sender, params object[] data)
    {
        if (/*[optional] here your domain condition that will indicate if the event should be raised*/)
        {
            // this is a best practice to deal with multi-threading situations
            var _succeed = this.Succeed;
            var args = new CustomEventArgs(data);
    
            _succeed(sender, args);
        }
    
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen people define their events like this: public event EventHandler<EventArgs> MyEvent =
I have an event fired when some button is being clicked private void set_Opacity(object
I have a problem with raising an event on a mocked object. I am
I have a class that exposes cancellable events according to the convention of raising
I have event manager process that dispatches events to subscribers (e.g. http_session_created, http_sesssion_destroyed). If
I need somebody with high skills in threading and event raising. I have an
I have a Sender class that sends a Message on a IChannel : public
I have a method in a class that performs several tasks (calling other methods,
I have a class with an indexer property, with a string key: public class
I have a class MyCol, which inherits from ObservableCollection(Of T). It overrides InsertItem method

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.