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?
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.
Check out the test framework that comes with NSB, it's using Rhino underneath: http://nservicebus.com/UnitTesting.aspx. there are several samples in the download that use it with NUnit. You can get it to work under MSTest as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With