I have a MessageHandler with a custom attribute on it. The behavior I'm testing does something based on the value of that attribute. The NServicebus documentation only gives unit tests examples of a behavior based on a mocked handler. How can I use my own fake or is there an other way I should test my behavior?
// A fake message handler containing the attribute
[ExecuteHandler(true)]
class FakeHandler : IHandleMessages<FakeCommand>
{
public Task Handle(FakeCommand message, IMessageHandlerContext context) => Task.CompletedTask;
}
[Test]
public async Task Invoke_GivenHandlerWithoutAttribute_InvokeNext()
{
// Given
var handlerExecuted = false;
var context = new TestableMessageHandlerContext
{
// I want to add my fake handler to the context so I can test if it gets executed. Problem is that I can't cast my handler to type MessageHandler
// MessageHandler = new FakeHandler(),
}
// When
await _behavior.Invoke(context, () =>
{
handlerExecuted = true;
return Task.CompletedTask;
});
// Then
handlerExecuted.Should().BeTrue();
}