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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:57:49+00:00 2026-05-17T15:57:49+00:00

I have this code in my app .NET application using NServiceBus: Bus.Send<IServiceStarted>(e => {

  • 0

I have this code in my app .NET application using NServiceBus:

 Bus.Send<IServiceStarted>(e =>
                             {
                                  e.ServiceInfo = ReadServiceInfo();
                                  e.EventTime = DateProvider.Now;
                             });

How would you go about unit-testing such a code?

  • 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-17T15:57:50+00:00Added an answer on May 17, 2026 at 3:57 pm

    As long as your Bus variable is an IBus instance, you can simply provide a mocked IBus implementation to the class that contains this method, and verify that Send was called on it (with the appropriate delegate, if you so desire) after the method was invoked for testing. Beyond that, you’re getting into testing Bus.Send itself, which is not something you should be doing.

    public class ClassThatSendsMessages
    {
        private readonly IBus _bus;
        public ClassThatSendsMessages(IBus bus /*, ... */)
        {
            _bus = bus;
            /* ... */
        }
    
        public void CodeThatSendsMessage()
        {
            /* ... */
            _bus.Send<IMyMessage>(mm => mm.Property1 = "123");
            /* ... */
        }
    }
    
    public class ClassThatTestsClassThatSendsMessages
    {
        public void CallCodeThatSendsMessage()
        {
            //Mock IBus object here
            var objectToTest = new ClassThatSendsMessages(mockedIBus /*, ... */)
    
            objectToTest.CodeThatSendsMessage();
    
            //Verify that IBus mock's Send() method was called
        }
    }
    

    There are two ways to approach testing the delegate’s logic: you can try to break down the provided expression tree, or you can change it to a named method and pass it that way. I’ve never gotten deep into expressions, so I’ll provide an example of the latter:

    public static class MyMessageBuilder
    {
        public static void Build(IMyMessage message) { /* ... */ }
    }
    
    public class ClassThatTestsMyMessageBuilder
    {
        public void CallCodeThatBuildsMessage()
        {
            var message = Test.CreateInstance<IMyMessage>(MyMessageBuilder.Build);
    
            //Verify message contents
        }
    }
    

    Usage:

    public class ClassThatSendsMessages
    {
        private readonly IBus _bus;
        public static Action<IMyMessage> BuildMessage { private get; set; }
        public ClassThatSendsMessages(IBus bus /*, ... */)
        {
            _bus = bus;
            /* ... */
        }
    
        public void CodeThatSendsMessage()
        {
            /* ... */
            _bus.Send<IMyMessage>(mm => BuildMessage (mm));
            /* ... */
        }
    }
    

    I’m not aware of a container that can do delegate injection to constructors, but then I haven’t looked very hard either, so that might be an even better way of setting the delegate.

    EDIT

    As I’ve recently run into this issue in my own tests, and I don’t really like having to pull the method out into its own builder. So I set out to discover if I could test the delegate “in place”. It turns out that you can:

    public class ClassThatTestsClassThatSendsMessages
    {
        public void CallCodeThatSendsMessage()
        {
        Action<IMyMessage> messageAction = null;
    
        //Mock IBus object here
        mockedIBus.Setup(b => b.Send(Args.IsAny<Action<IMyMessage>>()))
            .Callback((Action<IMyMessage> a) => messageAction = a);
        var myMessage = Test.CreateInstance<IMyMessage>();
    
        var objectToTest = new ClassThatSendsMessages(mockedIBus /*, ... */)
    
        //Run the code that sends the message
        objectToTest.CodeThatSendsMessage();
        //Run the code that populates the message
        messageAction(myMessage);
    
        //Verify expectations on Setups
        //Verify the message contents;
        }
    }
    

    There are tradeoffs here – pulling the message builder out into an interface is certainly more compliant with SRP than leaving it as an inline delegate (as the test clearly demonstrates: you have to Act twice in order to test all the code). However, it presents benefits in both code size and readability.

    Additionally, this code is Moq-specific; I don’t know whether it’s possible to get the delegate back from a RhinoMocks mock, or what the syntax would be for that.

    • 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.