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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:33:15+00:00 2026-06-02T23:33:15+00:00

I have a command handler that invokes an operation on a domain object which

  • 0

I have a command handler that invokes an operation on a domain object which in turn fires an event when the operation has been executed. I’d like to test that an event handler receives the event when the corresponding command has been sent (see below, some code omitted for brevity). The event handler (MyEventConsumer.Consume) is never invoked even though the event message is published on the bus (loopback bus in this case). Any ideas?

//Test
[TestFixture]
public class TestSendCommandReceiveEvent
{
    [Given]
    public void installation_of_infrastructure_objects()
    {
        container.Register(Component.For<MyEventConsumer>().UsingFactoryMethod(() => new MyEventConsumer(_received)));
        container.Register(
        Component.For<IServiceBus>()
        .UsingFactoryMethod(() => ServiceBusFactory.New(x => { x.ReceiveFrom("loopback://localhost/mt_client"); x.Subscribe(conf => conf.LoadFrom(container));                                                      })));
    }

    [When]
    public void sending_a_command()
    {
         var LocalBus = container.Resolve<IServiceBus>();
         LocalBus.Publish(new DoSomething(_aggregateId));
    }
    [Then]
    public void corresponding_event_should_be_received_by_consumer()
    {
        _received.WaitOne(5000).ShouldBeTrue();
    }
}
public class MyEventConsumer : Consumes<SomethingDone>.All
{
     private readonly ManualResetEvent _received;
     public MyEventConsumer(ManualResetEvent received)
     {
         _received = received;
     }
     public void Consume(SomethingDone message)
     {
         _received.Set();
     }
}

//Command handler
public class DoSomethingCommandHandler : Consumes<DoSomething>.All where T:class
{
    public void Consume(DoSomething message)
    {
       var ar = Repository.GetById<SomeAR>(message.ArId);
       ar.DoSomething();
       Repository.Save(ar, Guid.NewGuid(), null);
    }
}
//Domain object
public class SomeDomainObject : AggregateBase
{
    public void DoSomething()
    {
       RaiseEvent(new SomethingDone(Id, 1));
    }
}
  • 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-02T23:33:17+00:00Added an answer on June 2, 2026 at 11:33 pm

    This passes for me:

    // Copyright 2012 Henrik Feldt
    //  
    // Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
    // this file except in compliance with the License. You may obtain a copy of the 
    // License at 
    // 
    //     http://www.apache.org/licenses/LICENSE-2.0 
    // 
    // Unless required by applicable law or agreed to in writing, software distributed 
    // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
    // CONDITIONS OF ANY KIND, either express or implied. See the License for the 
    // specific language governing permissions and limitations under the License.
    
    using System;
    using System.Threading;
    using Castle.MicroKernel.Registration;
    using Castle.Windsor;
    using Magnum.Extensions;
    using Magnum.TestFramework;
    using MassTransit;
    using NUnit.Framework;
    
    namespace ConsoleApplication11
    {
        [TestFixture]
        public class TestSendCommandReceiveEvent
        {
            ManualResetEventSlim _received = new ManualResetEventSlim(false);
            IWindsorContainer _container;
    
            [Given]
            public void installation_of_infrastructure_objects()
            {
                _container = new WindsorContainer();
                _container.Register(
                    Component.For<IServiceBus>()
                        .UsingFactoryMethod(() => ServiceBusFactory.New(x =>
                            {
                                x.ReceiveFrom("loopback://localhost/mt_client");
                                x.Subscribe(conf =>
                                    {
                                        conf.Consumer(() => new MyEventConsumer(_received));
                                        conf.Consumer(() => new MyCmdConsumer());
                                    });
                            })));
    
                when();
            }
    
            public void when()
            {
                var localBus = _container.Resolve<IServiceBus>();
                // wait for startup
                localBus.Endpoint.InboundTransport.Receive(c1 => c2 => { }, 1.Milliseconds()); 
    
                localBus.Publish(new DoSomething());
            }
    
            [Then]
            public void corresponding_event_should_be_received_by_consumer()
            {
                _received.Wait(5000).ShouldBeTrue();
            }
        }
    
        [Serializable]
        public class DoSomething
        {
        }
    
        [Serializable]
        public class SomethingDone
        {
        }
    
        public class MyEventConsumer : Consumes<SomethingDone>.All
        {
            readonly ManualResetEventSlim _received;
    
            public MyEventConsumer(ManualResetEventSlim received)
            {
                _received = received;
            }
    
            public void Consume(SomethingDone message)
            {
                _received.Set();
            }
        }
    
        public class MyCmdConsumer : Consumes<DoSomething>.Context
        {
            public void Consume(IConsumeContext<DoSomething> ctx)
            {
                Console.WriteLine("consumed cmd");
                ctx.Bus.Publish(new SomethingDone());
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to have a command handler for a ToggleButton that can take multiple
I have a command (cmd1) that greps through a log file to filter out
I have a command which lists Weblogic instances directories on a server.I want to
I have a command line script that uses the Django ORM and MySQL backend.
I have a command line that I'm trying to modify to remove some of
I have a relatively simple page that has a couple of LI entries that
I have a server than is a command handler process. It receives messages over
I have a C# GUI application that references a Managed C++ project, which requires
I have turned computer monitor off using this command SendMessage(f.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)(turnOff ?
For security reasons (I'm a developer) I do not have command line access to

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.