Reputation: 31750
I want to test that handling a given instance of a message causes the handling to fail. However I can't see a way of doing this, as any exceptions thrown within the handler are not bubbled up to the testing code, and there doesn't appear to be anything built-in for this.
Ideally I'd like to do this:
Test.Handler<TransactionCreatedHandler>()
.ExpectFailure()
.OnMessage(financialTransaction, Guid.NewGuid().ToString());
I know the correct thing is to remove any processing code out of the handler and test that in isolation, but I'd still like to know if there's a way to do this.
Does anyone have any ideas?
Upvotes: 2
Views: 269
Reputation: 31750
I have worked around this behavior by wrapping the Test.Handler in a try and catching a TargetInvocationException
, then asserting against the InnerException
.
try
{
Test.Handler<TransactionCreatedHandler>()
.OnMessage(financialTransaction, Guid.NewGuid().ToString());
}
catch (TargetInvocationException ex)
{
// Asserts against ex.InnerException
...
}
This obviously creates the risk that changes to NSB implementation will break these tests though it's one I'm willing to take on.
Incorporating comment from Daniel Marbach:
In version 5 and higher the behaviour changed. The testing library no longer throws a TargetInvocationException but the actual exception that happened in the handle method. Just a heads up –
Upvotes: 2