diff --git a/src/NServiceBus.IntegrationTesting.Tests/When_capturing_handlers_invocations.cs b/src/NServiceBus.IntegrationTesting.Tests/When_capturing_handlers_invocations.cs new file mode 100644 index 00000000..b0dc9306 --- /dev/null +++ b/src/NServiceBus.IntegrationTesting.Tests/When_capturing_handlers_invocations.cs @@ -0,0 +1,104 @@ +using System; +using System.Threading.Tasks; +using NUnit.Framework; + +namespace NServiceBus.IntegrationTesting.Tests +{ + public class When_capturing_handlers_invocations + { + class TestMessage : IMessage + { + + } + + interface IMessageInterface: IMessage + { + + } + + class InheritedMessage : IMessageInterface + { + + } + + class TestMessageHandler : IHandleMessages + { + public Task Handle(TestMessage message, IMessageHandlerContext context) + { + return Task.CompletedTask; + } + } + + class MessageInterfaceHandler : IHandleMessages + { + public Task Handle(IMessageInterface message, IMessageHandlerContext context) + { + return Task.CompletedTask; + } + } + + [Test] + public void MessageWasProcessedByHandler_condition_should_match_type() + { + var scenarioContext = new IntegrationScenarioContext(); + scenarioContext.CaptureInvokedHandler(new HandlerInvocation() + { + Message = new TestMessage(), + EndpointName = "fake-endpoint", + HandlerType = typeof(TestMessageHandler), + HandlingError = null, + MessageType = typeof(TestMessage) + }); + + Assert.IsTrue(scenarioContext.MessageWasProcessedByHandler()); + } + + [Test] + public void MessageWasProcessedByHandler_condition_should_match_base_type() + { + var scenarioContext = new IntegrationScenarioContext(); + scenarioContext.CaptureInvokedHandler(new HandlerInvocation() + { + Message = new InheritedMessage(), + EndpointName = "fake-endpoint", + HandlerType = typeof(MessageInterfaceHandler), + HandlingError = null, + MessageType = typeof(InheritedMessage) + }); + + Assert.IsTrue(scenarioContext.MessageWasProcessedByHandler()); + } + + [Test] + public void MessageWasProcessed_condition_should_match_type() + { + var scenarioContext = new IntegrationScenarioContext(); + scenarioContext.CaptureInvokedHandler(new HandlerInvocation() + { + Message = new TestMessage(), + EndpointName = "fake-endpoint", + HandlerType = typeof(TestMessageHandler), + HandlingError = null, + MessageType = typeof(TestMessage) + }); + + Assert.IsTrue(scenarioContext.MessageWasProcessed()); + } + + [Test] + public void MessageWasProcessed_condition_should_match_base_type() + { + var scenarioContext = new IntegrationScenarioContext(); + scenarioContext.CaptureInvokedHandler(new HandlerInvocation() + { + Message = new InheritedMessage(), + EndpointName = "fake-endpoint", + HandlerType = typeof(MessageInterfaceHandler), + HandlingError = null, + MessageType = typeof(InheritedMessage) + }); + + Assert.IsTrue(scenarioContext.MessageWasProcessed()); + } + } +} \ No newline at end of file diff --git a/src/NServiceBus.IntegrationTesting.Tests/When_capturing_sagas_invocations.cs b/src/NServiceBus.IntegrationTesting.Tests/When_capturing_sagas_invocations.cs new file mode 100644 index 00000000..ba31d646 --- /dev/null +++ b/src/NServiceBus.IntegrationTesting.Tests/When_capturing_sagas_invocations.cs @@ -0,0 +1,101 @@ +using System; +using System.Threading.Tasks; +using NUnit.Framework; + +namespace NServiceBus.IntegrationTesting.Tests +{ + public class When_capturing_sagas_invocations + { + class TestMessage : IMessage + { + } + + interface IMessageInterface : IMessage + { + } + + class InheritedMessage : IMessageInterface + { + } + + class TestSaga : Saga, + IHandleMessages, + IHandleMessages + { + internal class Data : ContainSagaData + { + } + + protected override void ConfigureHowToFindSaga(SagaPropertyMapper mapper) + { + /**/ + } + + public Task Handle(TestMessage message, IMessageHandlerContext context) + { + return Task.CompletedTask; + } + + public Task Handle(IMessageInterface message, IMessageHandlerContext context) + { + return Task.CompletedTask; + } + } + + [Test] + public void MessageWasProcessedBySaga_condition_should_match_type() + { + var scenarioContext = new IntegrationScenarioContext(); + scenarioContext.CaptureInvokedSaga(new SagaInvocation() + { + Message = new TestMessage(), EndpointName = "fake-endpoint", SagaType = typeof(TestSaga), MessageType = typeof(TestMessage) + }); + + Assert.IsTrue(scenarioContext.MessageWasProcessedBySaga()); + } + + [Test] + public void MessageWasProcessedBySaga_condition_should_match_base_type() + { + var scenarioContext = new IntegrationScenarioContext(); + scenarioContext.CaptureInvokedSaga(new SagaInvocation() + { + Message = new InheritedMessage(), EndpointName = "fake-endpoint", SagaType = typeof(TestSaga), MessageType = typeof(InheritedMessage) + }); + + Assert.IsTrue(scenarioContext.MessageWasProcessedBySaga()); + } + + [Test] + public void MessageWasProcessed_condition_should_match_type() + { + var scenarioContext = new IntegrationScenarioContext(); + scenarioContext.CaptureInvokedSaga(new SagaInvocation() + { + Message = new TestMessage(), + EndpointName = "fake-endpoint", + SagaType = typeof(TestSaga), + HandlingError = null, + MessageType = typeof(TestMessage) + }); + + Assert.IsTrue(scenarioContext.MessageWasProcessed()); + } + + [Test] + public void MessageWasProcessed_condition_should_match_base_type() + { + var scenarioContext = new IntegrationScenarioContext(); + scenarioContext.CaptureInvokedSaga(new SagaInvocation() + { + Message = new InheritedMessage(), + EndpointName = "fake-endpoint", + SagaType = typeof(TestSaga), + HandlingError = null, + MessageType = typeof(InheritedMessage) + }); + + Assert.IsTrue(scenarioContext.MessageWasProcessed()); + } + } +} \ No newline at end of file diff --git a/src/NServiceBus.IntegrationTesting/IntegrationScenarioContext.cs b/src/NServiceBus.IntegrationTesting/IntegrationScenarioContext.cs index c188c442..e6c74e12 100644 --- a/src/NServiceBus.IntegrationTesting/IntegrationScenarioContext.cs +++ b/src/NServiceBus.IntegrationTesting/IntegrationScenarioContext.cs @@ -70,18 +70,18 @@ public bool SagaWasCompleted() where TSaga : Saga public bool MessageWasProcessed() { - return invokedHandlers.Any(invocation => invocation.MessageType == typeof(TMessage)) - || invokedSagas.Any(invocation => invocation.MessageType == typeof(TMessage)); + return invokedHandlers.Any(invocation => typeof(TMessage).IsAssignableFrom(invocation.MessageType)) + || invokedSagas.Any(invocation => typeof(TMessage).IsAssignableFrom(invocation.MessageType)); } public bool MessageWasProcessedBySaga() { - return invokedSagas.Any(i => i.SagaType == typeof(TSaga) && i.MessageType == typeof(TMessage)); + return invokedSagas.Any(i => i.SagaType == typeof(TSaga) && typeof(TMessage).IsAssignableFrom(i.MessageType)); } public bool MessageWasProcessedByHandler() { - return invokedHandlers.Any(i => i.HandlerType == typeof(THandler) && i.MessageType == typeof(TMessage)); + return invokedHandlers.Any(i => i.HandlerType == typeof(THandler) && typeof(TMessage).IsAssignableFrom(i.MessageType)); } public bool HasFailedMessages()